• 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: Retained items and autorelease pool
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Retained items and autorelease pool


  • Subject: Re: Retained items and autorelease pool
  • From: Carlos Weber <email@hidden>
  • Date: Tue, 7 Aug 2001 09:29:27 -1000

On Tuesday, August 7, 2001, at 08:54 , Erik M. Buck wrote:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
... much code here...
... in some subroutine somewhere:
// myString is a loose global variable, not a class member of
anything
myString = [[NSString stringWithCString:someString] retain];
... much other code here ...
... back in earlier routine:
[pool release];

Did I just lose myString? If so, how do I allocate things that don't get
cleaned up by releasing the pool?

You do not loose myString because it was never in pool because it was never
autoreleased. This is not hard stuff. RTFM.

Okay. Now, either I am REALLY confused (easy because I am a relative Cocoa beginner) or you may need to have another go at TFM. Here's my interpretation of why myString stays around, in the above code snippet:

myString is returned by +stringWithString, a so-called "convenience constructor". As such, +stringWithString conforms to the Cocoa convention of returning an AUTORELEASED object. So, my string comes back with a retain count of 1, and has been added to whatever autorelease pool is on top of the current thread's autorelease pool stack (in this case the one created in the code snippet itself). Next, the programmer calls -retain on the newly created string object, and bumps its count to 2.

Then, at the end of the code snippet, when [pool release] is executed, all its objects (including myString) get their retain counts decremented. Some objects, whose retain counts went to 0, are deallocated and disappear, BUT NOT myString, whose retain count is still 1. Also, pool itself is deallocated and destroyed, along with the references it kept to its objects. Thus, myString sticks around with a retain count of 1 and is not in any autorelease pool.

So, in AppKit programming (where there is ALWAYS an autorelease pool even if the programmer didn't create one), when you use a convenience constructor you must -retain or -copy the returned object IF you want to keep it for longer than the current scope (block, function, etc).

If I am substantially wrong about this then I am going to have to go back and relearn almost all of what I thought I knew about Cocoa...


References: 
 >Re: Retained items and autorelease pool (From: "Erik M. Buck" <email@hidden>)

  • Prev by Date: Re: Admin programs and password
  • Next by Date: Re: Retained items and autorelease pool
  • Previous by thread: Re: Retained items and autorelease pool
  • Next by thread: Localizing
  • Index(es):
    • Date
    • Thread