Re: Nib ownership and retain count
Re: Nib ownership and retain count
- Subject: Re: Nib ownership and retain count
- From: Ian was here <email@hidden>
- Date: Thu, 24 Jan 2008 12:48:24 -0800 (PST)
Yes, you are correct about dealloc not getting called
without the [self release] in the init method.
According to the documentation, NSNib has several
benefits over NSBundle (which is what I was using
before), mainly, its better optimized when
instantiating multiple copies of the same Nib file.
I overrode my object's release method and released the
top level objects when my retain count hit one, hoping
that whatever Nib object was retaining my object would
release it, but that didn't work.
>From what I gathered from the documentation, the
returned array of top level objects are autoreleased,
meaning that they would need to be retained by the
array containing them.
--- Keary Suska <email@hidden> wrote:
> 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
>
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
_______________________________________________
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