Re: NSTimer retain and release questions
Re: NSTimer retain and release questions
- Subject: Re: NSTimer retain and release questions
- From: Randall Meadows <email@hidden>
- Date: Wed, 10 Sep 2008 13:15:33 -0600
On Sep 10, 2008, at 12:44 PM, Bill wrote:
On Sep 10, 2008, at 11:37 AM, Bill Bumgarner wrote:
On Sep 10, 2008, at 11:21 AM, Kyle wrote:
The documentation for this autoreleased method:
+ (NSTimer *)scheduledTimerWithTimeInterval:
(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector
userInfo:(id)userInfo repeats:(BOOL)repeats
states: "If repeats is NO, the timer will be invalidated after it
fires."
If I call this autoreleased method, and set repeats to YES, does
that imply I do not need to retain the object if I want to
invalidate it later? In other words, is this autoreleased method
autoreleased only when repeats is NO, otherwise it is not
autoreleased?
Just want to make sure I understand it correctly. Thanks in
advance!
If you receive an autoreleased object, you must retain it if you
want to keep it. Doesn't matter what the object is.
That's what I thought also. But for NSTimer objects, it seems as if
the way to release them is to invalidate them.
Nope, those are two distinct, unrelated concepts.
In the above autoreleased method, if repeats is YES, the object is
not invalided,
True.
implying it is not autoreleased.
Non sequitur, in this case, and as a standalone statement, false.
If repeats is NO, then the object is autoreleased.
Again, unrelated. You need to separate "valid/invalid" and "retain/
autorelease" in your brain. There is no relation whatsoever between
them. (OK, the pedant in me is screaming: if an NSTimer object is
released (completely, its retain goes to 0), then one might argue that
that object is now "invalid", but only in this sense: NSObject *object
= [[[NSObject alloc] init] release], not "invalid" in the context of
NSTimers.
An invalid timer is one that has gone off (or you've cancelled
manually), and is set not to repeat. Its retain count is a completely
different matter.
Part of the reason I'm asking is because originally I used the above
method with a retain (and released later with the invalidate
method), and then ran my app with Leaks, and Leaks told me that the
timer was leaking. I removed the retain from my code, and Leaks
stopped reporting a leak.
So I'm not sure what is correct.
Without seeing your actual code that was complaining, I can't say what
was happening, but NSTimers follow the same memory management rules as
any other NSObject. The fact that it has another property (in this
case, its validity), is orthogonal to how you manage its lifetime.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden