Re: [Newbie] reference count
Re: [Newbie] reference count
- Subject: Re: [Newbie] reference count
- From: publiclook <email@hidden>
- Date: Fri, 16 May 2003 14:31:35 -0400
On Friday, May 16, 2003, at 09:53 AM, kunikyo wrote:
[Deleted]
The first question is about the following message.
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]] init];
This message is called in -init and -awakeFromNib, so this message is
called
twice at the opening sequence.
Is it permitted that allocWithZone of the same array is called twice?
Reference count kept 1 after twice call of allocWithZone.
It is not called twice. When objects are unarchived from a nib, the
-initWithCoder: initializer is called. -init is not called in that
context but -awakeFronNib is called.
Alternatively, if the object in question is initialized with -init
(because it is created programatically rather than in a nib) then the
object still needs an instance of mutable array to store documents.
There are two mutually exclusive paths through the code and both paths
need to allocate a mutable array.
I think that we need allocWithZone of -awakeFromNib but
we do not need allocWithZone of -init.
The allocWithZone of -init does not make sense to me.
The second question is that _myOpenDocuments was not released
after all.
We find [_myOpenDocuments release]; in - (void)dealloc,
but this was not invoked after selecting Quit on menu.
I am afraid of memory leak.
All memory allocated and all file pointers and all OS data structures
for an application are freed by the operating system when an
application terminates either normally or because of a signal or
un-handled exception. It would be less efficient if every object was
sent a -dealloc message right before the operating system deallocated
it anyway. [Note: the applicationWillTerminate: notification gives you
an opportunity to clean up explicitly if you insist].
The reason for even implementing -dealloc in the case you cite is
similar to the distinction between a programmatically initialized
instance and one unarchived from a nib. If you programatically
allocate an object, you probably programatically release it and in that
case its -dealloc will be called and it needs to explicitly clean-up.
If you unarchive an object from the main nib, the object usually has
the same lifetime as the application just like other main menu nib
objects such as menu items.
Thanks for the assistance!
Kunikyo
_______________________________________________
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.