Releasing objects allocated by the NIB

  • 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
  • On Sep 6, 2007, at 7:29 AM, Randy Thelen wrote:

    > 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?
    >
    <http://developer.apple.com/cgi-bin/search.pl?q=nib+file+memory+management&a
    mp;site=default_collection
    >

    mmalc
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today