Re: retain, release, autorelease (was Checkboxes and NSBox titles)
Re: retain, release, autorelease (was Checkboxes and NSBox titles)
- Subject: Re: retain, release, autorelease (was Checkboxes and NSBox titles)
- From: Art Isbell <email@hidden>
- Date: Wed, 20 Jun 2001 14:48:56 -1000
On Wednesday, June 20, 2001, at 09:39 AM, jgo wrote:
Could someone provide a snippet that shows or words that explain
how it works with containers/collections.
Most collections retain each object as the object is added
to the collection and release (hmm, maybe autorelease - doesn't
really matter for the purpose of this explanation) each object
as it is either removed from the collection or when the
collection is dealloc'ed. NSDictionary deals with keys and
values differently. So an object will have the same retain
count before and after its encounter with a collection. This
means that if you want the object to be automatically dealloc'ed
after it has been removed from the collection or the collection
has been dealloc'ed, add an autoreleased object to the
collection (OK) or release the object after it's been added to
the collection (better).
MyObject *object = [[MyObject alloc] init];
// object retain count = 1
NSArray *array = [[NSArray alloc] initWithObjects:object, nil];
// object retain count = 2
[object release];
// object retain count = 1
[array release];
// object retain count = 0
the comments say that collections retain their objects, I don't see
the many temporary objects being released... perhaps because the
example uses an auto-release pool? But if that is so, I don't see
what determines which pool objects get allocated in.
AppKit processes have a top-level autorelease pool created
automatically so it's behind the scenes. If you haven't created
other autorelease pools, all autoreleased objects will be in the
default autorelease pool. An autoreleased object is a member of
the innermost autorelease pool.
Art Isbell
Apple iServices Technical Support
http://www.apple.com/iservices/webobjectssupport/
+1-808-591-0836