Re: -awakeFromNib, application releasing...
Re: -awakeFromNib, application releasing...
- Subject: Re: -awakeFromNib, application releasing...
- From: James Quick <email@hidden>
- Date: Wed, 2 Jul 2003 11:45:53 -0400
I suspect that you may be misinterpreting the release retain model.
You say you implemented -(void)release. Typically an object inherits
release, since there is usually no need to respond to release
in a special way. What you probably intended to implement was
-(void)dealloc.
If as part of your object's allocation and initialization, additional
objects or
other resources are allocated, it is your responsibility to deallocate
them.
Each NSObject carries a retainCount. When the default implementation of
retain is called, the retainCount is incremented. When autorelease is
sent,
the object is added to the current NSAutorelease pool. The next pass
through
the run loop each object in the pool will be sent a release message.
The default
implementation of release decrements the retainCount. If retainCount
becomes 0
the object is sent dealloc, if retainCount is already 0 an error occurs.
So, the fact that you are implementing release rather than dealloc,
suggests
that you misunderstood the intended behavior.
An object may recieve many retain and release messages and never be
deallocated.
The Appkit, uses a number of collection objects to retain information
on all the
components in an application. Each time an object is added to an
array, for instance
it will receive retain. When it is deleted from the array it likewise
gets sent release.
How many times an object is retained or released is usually no business
of the object.
It does not have to care. It's only responsibility is implementing
dealloc so that
the final release frees the object's resources.
A typical dealloc
- (void) dealloc
{
[myName release];
[myArray release];
[super release];
}
Note that all Object resources you contain are still sent release
rather than
deallocate.
On Wednesday, July 2, 2003, at 10:37 AM, Jeremy Faller wrote:
All:
Hello all. I have encountered something in Cocoa, that I can't
explain, and would like to know why I'm so silly.
Starting from a New Cocoa Application (non-doc based).
Let's pretend I create an Obj-C class Foo, and write the -init,
-release, and and -awakeFromNib methods to just NSLog(@"....").
Additionally, I am going to load Foo.h into my MainMenu.nib file, and
instantiate a Foo there.
When the application runs, it just prints the following:
2003-07-02 07:32:31.895 Test[4585] In init!
2003-07-02 07:32:31.901 Test[4585] In awake!
2003-07-02 07:32:31.902 Test[4585] In release!
2003-07-02 07:32:32.005 Test[4585] In release!
If I eliminate the -awakeFromNib method, the -release is never called,
and my Foo object sticks around. What's going on, and why is -release
getting called twice? (I have tried this on 2 machines running
10.2.6, and get the same results on both.)
Jeremy Faller
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.