Re: Nib ownership and retain count
Re: Nib ownership and retain count
- Subject: Re: Nib ownership and retain count
- From: Keary Suska <email@hidden>
- Date: Thu, 24 Jan 2008 11:39:01 -0700
- Thread-topic: Nib ownership and retain count
on 1/24/08 10:23 AM, email@hidden purportedly said:
> I have an interesting issue. I noticed that when I make an object a Nib file's
> owner, that my object's retain count increases by one. So, when I release my
> object, it doesn't get deallocated. I found that the following code works. I
> was just wondering if there was a better way.
There is likely better code than what you show, but the general theoretical
approach is typical. I prefer to use NSBundle's
loadNibFile:externalNameTable:withZone:, as it collapses loading and
instantiating into one step. There are also supposed to be other side
benefits to this approach, but I don't recall them at the moment.
I typically have an -unLoad method that releases the top level objects
array, which must be called before the owner of the nib loading object
releases that object.
Note also that objects won't get deallocated until the current autorelease
pool is drained/released. This is normally not an issue but can come up in
certain situations, particularly when using modal sessions.
Some issues with your code though. In your init:
> [topLevelNibObjects copy];
All this accomplishes is a potentially significant memory leak.
In your dealloc:
> [self retain]; // Must increase my object's retain count so it doesn't
> crash.
This is probably necessary because you are over-releasing. You probably
don't need the [self release] in your init. Your retain count is likely
increasing because one of the nib objects is retaining it. When that object
is released, it will release its retain. Don't force it's hand. As you can
see, you are likely compensating for your own lack of observance to memory
management rules.
You are probably ding the release in init because otherwise your -dealloc is
not getting called. Is this correct? Not a good approach, IMHO. If you want
a simpler interface, you should override -release and -autorelease to
release your top level objects first.
Best,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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