快速提问,Instruments 报告此处存在泄漏...
MyViewController *myVC = [[MyViewController alloc] initWithNibName:@"myView" bundle:nil];
[self.navigationController pushViewController:myVC animated:YES]; //<<<<---- !00% leak according to Instruments
[myVC release];
我知道 myVC 由导航 Controller 保留,所以我假设导航 Controller 在 View 从导航堆栈弹出时释放它们?
此外,在我的一个循环中还有另一个棘手的问题,静态分析器报告此处存在潜在泄漏...
//Walk through the scheduled alarms and create notifications
NSMutableArray *fireDates = [[NSMutableArray alloc] init];
for(NSDate *fireDate in fireDates) //<<<<---- Static analyzer is reporting potential leak here
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
{
[fireDates release];
return;
}
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:@"%@", alarm.Label];
localNotif.alertAction = NSLocalizedString(@"Launch", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.userInfo = infoDict;
localNotif.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
[fireDates release];
我需要以某种方式释放 fireDate 吗?
预先感谢您的帮助!
最佳答案
这些片段都很好,就像片段一样。如果没有看到您的完整代码,就不可能说您是否没有在其他地方做一些愚蠢的事情。你有没有释放过你的 navigationController(可能在你的应用委托(delegate) -dealloc
中)?这是一次泄漏,意义不大,但可能是触发第一个警告的原因。
编辑:关于第二个片段,代码看起来不错(尽管快捷方式返回大小写会打扰一些编码人员,他们宁愿看到break
语句)。静态分析器可能会因为条件返回中缺少 [localNotif release]
(即使它显然是不必要的)而感到困扰。
关于iphone - 为什么 Instruments 在此代码中报告内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3526233/