Re: NSTimer memory management
Re: NSTimer memory management
- Subject: Re: NSTimer memory management
- From: Dave Keck <email@hidden>
- Date: Wed, 22 Sep 2010 21:31:19 -0400
> Is this an over-release?
>
> timer = [ [NSTimer scheduledTimerWithTimeInterval: ...] retain];
> ...
> [timer invalidate];
> [timer release];
No, you're not over-releasing the timer.
> I've seen this pattern so many times I figured it was correct,
> although it doesn't seem to comply with the memory management rules
> (i. e. the timer is first implicitly released when invalidated, then
> explicitly released again).
It's a valid technique; when you call -invalidate, the run loop
releases the timer (to balance the implicit -retain that it made when
the timer was scheduled), but you still need to balance the explicit
-retain that you made when the timer was allocated.
> Can someone please help clarify? This is my full code:
>
> - (void)invalidateTimer
> {
> if ( [theTimer isValid] ) {
> [theTimer invalidate];
> [theTimer release]; // crashes here
> theTimer = nil;
> }
> }
>
> - (void)startTimer
> {
> [self invalidateTimer];
> theTimer = [[NSTimer scheduledTimerWithTimeInterval:300 target:self
> selector:@selector(timerFireMethod:) userInfo:nil repeats:NO] retain];
> }
Looks fine to me. Perhaps this is a multi-threading synchronization
issue? (Is more than one thread calling these methods?)
_______________________________________________
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