• 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: 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

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

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