Autorelease pools

⌘-R in Chief
Posts: 1,277
Joined: 2002.05
Post: #16
And it's still autoreleased properly because the autorelease pool has a reference to it. When the autorelease method is called (whether by your or any other method), it finds the pool on the top of the stack, and that pool keeps a reference to the object. When the pool is deallocated, it sends a release message to every object it has a reference to.
Quote this message in a reply
Posts: 34
Joined: 2009.01
Post: #17
Gillissie Wrote:Thanks for the additional info.

So what happens if you create a NSString object, then create another object using the same variable in the same scope? Does the first one still get autoreleased, or is it a memory leak? I would like to create several audio objects, and I would like to re-use the soundFilePath variable.
The real answer for this depends on what your definition of "create" is. If you "create" autoreleased objects, then your variable can be assigned and reassigned to your heart's content without causing a leak. If, however, you "create" retained objects, you must make sure to release the existing object in your variable before reassigning to a new object or you'll have leaks.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Is implementing autorelease code in constructor a good idea ? Bracer 4 4,803 Oct 13, 2009 12:41 PM
Last Post: SethWillits
  Track down an autorelease bug kendric 2 4,618 Jun 28, 2009 05:09 AM
Last Post: kendric