Alloc Mocks my World
Alloc Mocks my World
- Subject: Alloc Mocks my World
- From: Philip Dow <email@hidden>
- Date: Thu, 25 Aug 2005 10:43:26 +0200
I thought I had my head wrapped around alloc, but apparently not. As
far as I understand alloc, it produces an object with a retain count
of 1. If I create an object with the standard MyObject *newObject =
[[myobject alloc] init] the object will have a retain count of 1.
I am allocating and initializing an object like so:
JRLREntryCell *newCell = [[JRLREntryCell alloc] initWithEntry:
[[browseArrayController selectedObjects] objectAtIndex:0]];
The very next line of code reads: int retCount = [newCell
retainCount]; And what is the retain count? 3. Three! Not one. Not
even two. Three. How in the world is my allocation and initialization
producing a retain count of three?
The initialization is simple. I need the object to become the owner
of a nib file. I also need it to keep a week reference to the entry
I'm passing in via the initWithEntry: portion. So...
- (id) initWithEntry:(JournlerEntry*)representedEntry {
if ( self = [super init] ) {
entry = representedEntry;
[NSBundle loadNibNamed:@"EntryCell" owner:self];
}
return self;
}
Once the nib loads, I pass the weakly referenced entry to a custom
view in the nib file:
- (void) awakeFromNib {
[headerView setDisplayedEntry:entry];
}
Nothing I've read about object allocation and nib ownership suggests
that this code would produce an object with a retain count of three.
If declaring the object owner of the nib increased the retain count,
then maybe I would understand two. But in all the examples of seen,
no one seems to worry that something like this is happening. The
"Loading Resources" documentation doesn't mention this either. Does
anyone know how the retain count hits 3 then?
-Phil
http://phildow.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden