• 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: Memory Management Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management Question


  • Subject: Re: Memory Management Question
  • From: Seth Pellegrino <email@hidden>
  • Date: Thu, 5 Jul 2007 12:34:43 -0400


Thank you all for your help, my code is working great now.

Seth

On Jul 4, 2007, at 1:33 AM, James Bucanek wrote:

Seth Pellegrino <mailto:email@hidden> wrote (Tuesday, July 3, 2007 10:13 AM -0400):
Mainly my problem is with the NSTimer class. Whenever the user clicks pause in my application, I call -[invalidate] to stop the timer from firing (as there seems to be no -[pause] or similar). However, this causes the run loop to release the timer, so whenever I try to call -[isValid], my program crashes. As I type this, I realize that I should be also -[retain]ing my instance of the NSTimer class, but if I do so, is there a way to make an invalidated timer resume firing? Or would it be better to just use a BOOL isPaused and an if statement?

Handling repeating NSTimers is a little different than most objects. They are retained by the run loop while they are active, and essentially become useless once stopped.


Consequently, I find that it's easier to let the run loop manage their ownership and simply forget about it once you've stopped them.

When you need to start the timer, create it but let the run loop retain it. In your "begin" or "resume" routine (written in mail):

    if (myTimer==nil)
        myTimer = [NSTimer scheduledTimerWith...];

In your "stop" or "pause" routine, simply forget the timer and let the run loop dispose of it:

    [myTimer invalidate];
    myTimer = nil;

It's always safe to send messages to myTimer because either the timer will still be valid while it's running or myTimer will be nil once you've invalidated it.

Note that this only applies to repeating timers, which remain valid until you invalidate it. One-shot timers either need to be retained (because the run loop will release it once it has fired), or simply write your code so you don't need a reference to the timer at all.

--
James Bucanek


_______________________________________________

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: 
 >Re: Memory Management Question (From: James Bucanek <email@hidden>)

  • Prev by Date: Re: Simple authorization question
  • Next by Date: Re: Logging to text files
  • Previous by thread: Re: Memory Management Question
  • Next by thread: Re: Memory Management Question
  • Index(es):
    • Date
    • Thread