Re: [Newbie] reference count
Re: [Newbie] reference count
- Subject: Re: [Newbie] reference count
- From: publiclook <email@hidden>
- Date: Sun, 18 May 2003 08:54:53 -0400
I think kunikyo is right. In the MYDocumentManager from "Cocoa
Programming", the following causes an extra mutable dictionary
allocation:
- (void)_myInitInstanceVariables
/*" Initialize instance variables: called by -init and -awakeFromNib.
"*/
{
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
// Create array of open documents
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]] init];
// Set initial cascade point
_myWindowCascadePoint = NSMakePoint(0, NSMaxY(screenVisibleFrame));
}
- (id)init
/*" Designated initializer "*/
{
self = [super init];
if(nil != self)
{
[self _myInitInstanceVariables];
}
return self;
}
- (void)awakeFromNib
/*" Called automatically after receiver is fully loaded from a nib
file. "*/
{
[self _myInitInstanceVariables];
}
It seems that both -init and -awakeFromNib are called at startup.
-init is called first from
#0 -[MYDocumentManager init]
#1 -[NSCustomObject nibInstantiate] ()
#2 -[NSIBObjectData instantiateObject:] ()
#3 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] ()
#4 -[NSMenuItem keyEquivalentModifierMask] ()
#5 +[NSBundle(NSNibLoading)
_loadNibFile:nameTable:withZone:ownerBundle:] ()
#6 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] ()
#7 +[NSBundle(NSNibLoading) loadNibNamed:owner:] ()
#8 NSApplicationMain ()
#9 main (argc=1, argv=0xbffffc34)
followed by -awakeFromNib called from
#0 -[MYDocumentManager awakeFromNib]
#1 -[NSSet makeObjectsPerformSelector:] ()
#2 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] ()
#3 -[NSMenuItem keyEquivalentModifierMask] ()
#4 +[NSBundle(NSNibLoading)
_loadNibFile:nameTable:withZone:ownerBundle:] ()
#5 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] ()
#6 +[NSBundle(NSNibLoading) loadNibNamed:owner:] ()
#7 NSApplicationMain ()
#8 main (argc=1, argv=0xbffffc34)
Given this fact, the _myInitInstanceVariables method should have been
the following:
- (void)_myInitInstanceVariables
/*" Initialize instance variables: called by -init and -awakeFromNib.
"*/
{
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
// Create array of open documents
if(nil == _myOpenDocuments)
{
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]]
init];
}
// Set initial cascade point
_myWindowCascadePoint = NSMakePoint(0, NSMaxY(screenVisibleFrame));
}
There is other errata for MYDocumentManager at
http://www.cocoaprogramming.net/Errata.html
Now, the real question is why -init was called in the loading from nib
scenario ? Is it because there was no -initWithCoder: implementation ?
No. I just added conformance to the NSCoding protocol and -init was
still called.
I don't understand why -init was called in the loading from nib
scenario ?
On Sunday, May 18, 2003, at 04:57 AM, kunikyo wrote:
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.
_______________________________________________
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.