简体   繁体   English

Xcode:存储到“通知”中的对象的潜在泄漏

[英]Xcode: Potential leak of an object stored into 'notification'

I have an issue that makes my application crash, and I have tried everything to change it, but with no luck. 我遇到了一个使我的应用程序崩溃的问题,并且我已经尽一切努力对其进行了更改,但是没有运气。 So I hope that a new set of eyes could help me out a little bit. 因此,我希望新的眼睛能对我有所帮助。

Here is a view of my code: 这是我的代码的视图:

.h 。H

@property (nonatomic, retain) UILocalNotification *notification;

.m .m

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
    notification.alertBody = @"Skal du ikke træne i dag? Det tager kun 7 minutter!";
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

It gives me the following error when analyzing it: 分析时会出现以下错误:

  • Potential leak of an object stored into 'notification' 存储在“通知”中的对象的潜在泄漏

I really hope that one of you could help me out. 我真的希望你们中的一个能帮助我。 Thanks! 谢谢!

Similar to your other question, change: 与您的其他问题类似,更改:

UILocalNotification *notification = [[UILocalNotification alloc] init];

to: 至:

self.notification = [[UILocalNotification alloc] init];

and use self.notification instead of notification elsewhere. 并使用self.notification代替其他地方的notification As you are using a recent version of Xcode, ARC will be enabled by default. 当您使用最新版本的Xcode时,默认情况下将启用ARC。 If so, the answer above regarding using release is incorrect. 如果是这样,则以上关于使用release的答案是错误的。

NOTE: edited this answer to use property dot notation instead of accessing the ivar directly. 注意:编辑此答案以使用属性点表示法,而不是直接访问ivar。 See this SO answer for a little more background on that: Is self.iVar necessary for strong properties with ARC? 有关更多背景知识, 请参见此 SO答案: self.iVar对于ARC的强大属性是否必要?

You need to -release or -autorelease the new notification. 您需要-release-autorelease新通知。 The succinct way to do this is: 简洁的方法是:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UILocalNotification * notification =
      [[[UILocalNotification alloc] init] autorelease];
                                          ^^^^^^^^^^^

    notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
    notification.alertBody = @"Skal du ikke træne i dag? Det tager kun 7 minutter!";
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

The system relies (heavily) on naming conventions. 系统(很大程度上)依赖于命名约定。 An initializer (eg -init ), copier (copy, mutableCopy), and +new are examples of methods which return instances which you must release (or autorelease). 初始化程序(例如-init ),复印机(副本,mutableCopy)和+new是返回必须释放(或自动释放)实例的方法的示例。

Also note that UILocalNotification * notification = ... declares a new variable local to your method body, which shadows your notification property. 还要注意, UILocalNotification * notification = ...声明了一个位于方法主体局部的新变量,该变量UILocalNotification * notification = ...了您的notification属性。

You are allocating a local UILocalNotification but not releasing it. 您正在分配本地UILocalNotification但不释放它。 At least not in the code you posted. 至少不在您发布的代码中。 The analyzer caught you because it doesn't see the resource being released. 分析器抓住了您,因为它看不到资源被释放。 If you are releasing elsewhere, in a legit fashion the analyzer won't catch it. 如果要释放到其他地方,则分析仪将无法合法捕获它。

To fix this you should assign the local variable to your property to ensure the owner of the property (looks like app delegate) keeps a living reference to the notification. 要解决此问题,您应该将局部变量分配给您的媒体资源,以确保媒体资源的所有者(看起来像应用程序委托)始终保持对该通知的引用。

self.notification = notification;

And release before leaving the method so you ensure to balance your retain count. 在退出该方法之前,请先释放它,以确保平衡您的保留数。

[notification release];

Finally, once you are done using the notification you can nil out your property. 最后,使用通知完成后,您可以取消属性。 To release it from the app delegate. 从应用程序委托中释放它。 Be sure to do this once you are done using it. 使用完毕后,请务必执行此操作。

self.notification = nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM