Re: [Newbie] reference count
Re: [Newbie] reference count
- Subject: Re: [Newbie] reference count
- From: kunikyo <email@hidden>
- Date: Sun, 18 May 2003 17:57:18 +0900
Hi publiclook.
Thanks for the response!
On 2003.5.17, at 03:31 AM, publiclook wrote:
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.
When I traced the operation of the sample software, at first the -init
was called,
and then -awakeFromNib was called. So this is not the case.
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.
I think this is the case.
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]] init];
is called in each -init and -awakeFromNib. I think the one in the -init
is no use. Only one in the -awakeFromNib is enough.
There are two mutually exclusive paths through the code and both paths
need to allocate a mutable array.
I am sorry, I do not understand this sentence.
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].
I could understood this paragraph.
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.
On the case of sample software in question, the object is initialized by
-init and -awakeFromNib, but -dealloc is not called.
But I do not afraid of memory leak because memory is freed by the
operating
system when an application terminates as you explain.
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.