• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTimer memory management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTimer memory management


  • Subject: Re: NSTimer memory management
  • From: email@hidden
  • Date: Wed, 22 Sep 2010 19:24:18 -0600

- (void)invalidate

Discussion

This is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes and releases the timer, either just before the invalidate method returns or at some later point.



On Sep 22, 2010, at 5:38 PM, email@hidden wrote:

Hi,

Is this an over-release?

timer = [ [NSTimer scheduledTimerWithTimeInterval: ...] retain];
...
[timer invalidate];
[timer release];

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).This never caused a problem on my Intel,
but now I got several crash reports indicating an over-release on X86:

Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 com.apple.CoreFoundation 0x92752c94 CFRelease + 196
1 com.apple.CoreFoundation 0x927c97c1 __CFRunLoopTimerDeallocate + 33
2 com.apple.CoreFoundation 0x92752e31 _CFRelease + 353
3 myApp 0x0000878a 0x1000 + 30602 // =
invalidateTimer method


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];
}
_______________________________________________

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


_______________________________________________

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


References: 
 >NSTimer memory management (From: "email@hidden" <email@hidden>)

  • Prev by Date: Re: NSOperation and threadDictionary
  • Next by Date: Re: What's the point of @properties?
  • Previous by thread: NSTimer memory management
  • Next by thread: Re: NSTimer memory management
  • Index(es):
    • Date
    • Thread