Releasing objects allocated by the NIB
Releasing objects allocated by the NIB
- Subject: Releasing objects allocated by the NIB
- From: Randy Thelen <email@hidden>
- Date: Thu, 6 Sep 2007 07:29:03 -0700
Folks --
After a little experimentation, I've concluded that one should never
release an object loaded by way of a NIB. Is this conclusion
correct? If so, where is it documented? (I'd like to learn -other-
rules that may be related :-)
In my specific example, I have an class Foo, that I decided to place
into my Window's NIB file. That way, it would auto-magically be
allocated so that when my window controller ran, it would already
have a Foo to work with. Later, I changed the way Foo worked so that
whenever the window controller wanted to change the image Foo
rendered, it simply released the whole Foo and allocated a new Foo
instance. After making that change I noticed that closing the window
often caused a crash of my program. I didn't make the connection
immediately because the amount of churn in the code to make that
change was rather large. However, after looking at the following
output, I concluded that the NIB had an autorelease pool. When I
removed the object from the NIB file entirely and modified the code
to always allocate the Foo upon wakeFromNIB then the problem was
gone. Did I properly solve this problem? Is my conclusion (stated
above) correct?
$ malloc_history 16681 0x35bed0
Call [2] [arg=72]: thread_a000d000 |0x1 | start | _start |
NSApplicationMain | -[NSApplication run] | -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:] | _DPSNextEvent |
AEProcessAppleEvent | aeProcessAppleEvent | dispatchEventAndSendReply
(AEDesc const*, AEDesc*) | aeDispatchAppleEvent(AEDesc const*,
AEDesc*, unsigned long, unsigned char*) |
_NSAppleEventManagerGenericHandler | -[NSAppleEventManager
dispatchRawAppleEvent:withRawReply:handlerRefCon:] | -[NSApplication
(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] | -
[NSApplication(NSAppleEventHandling) _handleAEOpen:] | -
[NSApplication _doOpenUntitled] | -[NSDocumentController(NSInternal)
_openUntitled] | -[NSDocumentController
openUntitledDocumentAndDisplay:error:] | -[NSDocument showWindows] | -
[NSWindowController showWindow:] | -[NSWindowController window] | -
[NSWindowController loadWindow] | +[NSBundle(NSNibLoading)
loadNibFile:externalNameTable:withZone:] | +[NSBundle(NSNibLoading)
_loadNibFile:nameTable:withZone:ownerBundle:] | loadNib |
_decodeObject | _decodeObjectBinary | -[NSIBObjectData
initWithCoder:] | _decodeObject | _decodeObjectBinary | -[NSArray
initWithCoder:] | -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] |
_decodeObjectBinary | -[NSNibOutletConnector initWithCoder:] | -
[NSNibConnector initWithCoder:] | _decodeObject | _decodeObjectBinary
| -[NSButton initWithCoder:] | -[NSControl initWithCoder:] | -
[NSButtonCell(NSButtonCellPrivate) setControlView:] | -[NSButton
(NSButtonBorder) _clearMouseTrackingForCell:] | objc_msgSend |
_class_lookupMethodAndLoadCache | _cache_fill | _cache_malloc |
_calloc_internal | malloc_zone_calloc
Call [4] [arg=0]: thread_a000d000 |0x1 | start | _start |
NSApplicationMain | -[NSApplication run] | -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:] | _DPSNextEvent |
AEProcessAppleEvent | aeProcessAppleEvent | dispatchEventAndSendReply
(AEDesc const*, AEDesc*) | aeDispatchAppleEvent(AEDesc const*,
AEDesc*, unsigned long, unsigned char*) |
_NSAppleEventManagerGenericHandler | -[NSAppleEventManager
dispatchRawAppleEvent:withRawReply:handlerRefCon:] | -[NSApplication
(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] | -
[NSApplication(NSAppleEventHandling) _handleAEOpen:] | -
[NSApplication _doOpenUntitled] | -[NSDocumentController(NSInternal)
_openUntitled] | -[NSDocumentController
openUntitledDocumentAndDisplay:error:] | -[NSDocument showWindows] | -
[NSWindowController showWindow:] | -[NSWindowController window] | -
[NSWindowController loadWindow] | +[NSBundle(NSNibLoading)
loadNibFile:externalNameTable:withZone:] | +[NSBundle(NSNibLoading)
_loadNibFile:nameTable:withZone:ownerBundle:] | loadNib | -
[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] | -
[NSIBObjectData instantiateObject:] | -[NSWindowTemplate
nibInstantiate] | -[NSWindow setTitle:] | -[NSWindow
_dosetTitle:andDefeatWrap:] | -[NSTitledFrame setTitle:] | -
[NSFrameView titleCell] | -[NSTextFieldCell initTextCell:] |
objc_msgSend | _class_lookupMethodAndLoadCache | _cache_fill |
_cache_collect_free | free
Call [2] [arg=56]: thread_a000d000 |0x1 | start | _start |
NSApplicationMain | -[NSApplication run] | -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:] | _DPSNextEvent |
AEProcessAppleEvent | aeProcessAppleEvent | dispatchEventAndSendReply
(AEDesc const*, AEDesc*) | aeDispatchAppleEvent(AEDesc const*,
AEDesc*, unsigned long, unsigned char*) |
_NSAppleEventManagerGenericHandler | -[NSAppleEventManager
dispatchRawAppleEvent:withRawReply:handlerRefCon:] | -[NSApplication
(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] | -
[NSApplication(NSAppleEventHandling) _handleAEOpen:] | -
[NSApplication _doOpenUntitled] | -[NSDocumentController(NSInternal)
_openUntitled] | -[NSDocumentController
openUntitledDocumentAndDisplay:error:] | -[NSDocument showWindows] | -
[NSWindowController showWindow:] | -[NSWindowController window] | -
[NSWindowController loadWindow] | +[NSBundle(NSNibLoading)
loadNibFile:externalNameTable:withZone:] | +[NSBundle(NSNibLoading)
_loadNibFile:nameTable:withZone:ownerBundle:] | loadNib | -
[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] | -
[NSIBObjectData instantiateObject:] | -[NSCustomObject
nibInstantiate] | NSAllocateObject |
_internal_class_createInstanceFromZone | malloc_zone_calloc
$
Incidentally, I learned how to generate but not how to decode the
above goo here:
http://www.cocoadev.com/index.pl?DebuggingAutorelease
-- Randy
_______________________________________________
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