2 Replies Latest reply: Jan 25, 2011 6:16 PM by Den B.
Den B. Level 1 Level 1 (5 points)
Say, if I call the following code:

[NSTimer scheduledTimerWithTimeInterval:numberSeconds target:self selector:@selector(doTimeOutFunction:) userInfo:[NSNumber numberWithUnsignedInt:nValue] repeats:NO];

Is my guess correct that when I call [[NSNumber numberWithUnsignedInt:nValue]] the resulting object has an autorelease flag, right? If so, is it safe to call it for a timer that may fire way after the autorelease pool was cleared? And if not, how would re-write this code?

MacBook Pro 17", 2.66 GHz i7, 4GB 1067 DDR3 RAM, OS X 10.6.4 /// iPhone 4, 32GB, iOS 4
  • 1. Re: Question about autorelease functions
    RayNewbie Level 5 Level 5 (6,810 points)
    Den B. wrote:
    Is my guess correct that when I call \[NSNumber numberWithUnsignedInt:nValue\] the resulting object has an autorelease flag, right?

    Yes. The absence of the words "alloc", "new", or "copy" in the method name tell you that you don't own the returned object, and aren't responsible for releasing it; i.e. it's probably been autoreleased, but in any case you can't expect it to persist beyond the current event cycle. Be sure to read [Memory Management Rules|http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Mem oryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH] in the +Memory Management Programming Guide+ for more general details.

    If so, is it safe to call it for a timer that may fire way after the autorelease pool was cleared?

    In the case of [scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:|http://develo per.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSTim erClass/Reference/NSTimer.html#//appleref/occ/clm/NSTimer/scheduledTimerWithTimeInterval:target:selector:userInfo:repe ats:], yes, because the doc explicitly says so:
    userInfo
    The user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.

    \- Ray
  • 2. Re: Question about autorelease functions
    Den B. Level 1 Level 1 (5 points)
    Thanks, Ray. I appreciate your help.