1

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

@property (nonatomic, retain) UILocalNotification *notification;

.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!

  • You are not using the `notification` property in this method. Is this the code you wanted to post here? Also, the potential leak doesn't need to be related to the crash (and likely isn't). – Tricertops Jul 03 '13 at 17:48
  • My question to you is: Why you are not using ARC? If you don't know what it is, just turn it on ;) – Tricertops Jul 03 '13 at 17:49
  • As a side note: `60 * 60 * 24` is not guaranteed to give you right now +1 day! Time **is** evil, make sure to handle it correctly! – JustSid Jul 03 '13 at 21:50

3 Answers3

0

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. As you are using a recent version of Xcode, ARC will be enabled by default. If so, the answer above regarding using release is incorrect.

NOTE: edited this answer to use property dot notation instead of accessing the ivar directly. See this SO answer for a little more background on that: Is self.iVar necessary for strong properties with ARC?

Community
  • 1
  • 1
stevekohls
  • 2,214
  • 23
  • 29
  • using ivars directly is a great way to leak objects. – Matthias Bauch Jul 03 '13 at 20:06
  • Agreed there are issues with using ivars directly. I almost exclusively use dot notation (self.x) in my code. Was just following the ivar notation from an answer I gave on another of the OPs questions. There's a presentation from Apple I think that sums up the issues with properties vs ivars, but I don't know where to find it. Corrected my answer to use propeties instead of ivars. – stevekohls Jul 03 '13 at 21:15
0

You need to -release or -autorelease the new notification. 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 (e.g. -init), copier (copy, mutableCopy), and +new are examples of methods which return instances which you must release (or autorelease).

Also note that UILocalNotification * notification = ... declares a new variable local to your method body, which shadows your notification property.

justin
  • 104,054
  • 14
  • 179
  • 226
-1

You are allocating a local UILocalNotification but not releasing it. 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
feliun
  • 181
  • 6