iphone - 为什么 Instruments 在此代码中报告内存泄漏?

标签 iphone ipad memory-management ios instruments

快速提问,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/

相关文章:

java - Java中对对象进行垃圾回收的方法

java - 调整 Java GC,使其立即抛出 OOME,而不是不确定地减慢速度

iphone - 如何知道为什么应用程序在模拟器中崩溃

ios - Objective-C:如何从子类访问父私有(private)属性?

ios - 在更改 View (Segue) 时显示事件指示器并为下一个 View (API) 加载数据

ios - Ipad 中的 Infinea Tab 集成

ios - 什么是实现事件指标的好概念

iPhone-应用程序崩溃使用 SKPSMTPMessage

iOS Metal : Unable to create compute pipeline state with function

c++ - 为什么当新数组、调整 vector 大小和push_back到 vector 时会出现bad_alloc?