Is dealloc called for an object instantiated with IB?

  • In Hillegass book chapter 4, there is a class called AppController instantiated with IB. In the file AppController.m there is the following method:

    - (void)dealloc
    {
        NSLog(@"dealloc");
        [speechSynth release];
        [super dealloc];
    }
    @end

    However when I run the application and quit it, I never see the text "dealloc" appear in the log window. Does this mean that this method is never called?

          ____________________________________________________________________________________
    Catch up on fall's hot new shows on Yahoo! TV. Watch previews, get listings, and more!
    http://tv.yahoo.com/collections/3658
  • On 29/09/2007, Bob Ueland <bobueland...> wrote:
    > In Hillegass book chapter 4, there is a class called AppController instantiated with IB. In the file AppController.m there is the following method:
    >
    > - (void)dealloc
    > {
    > NSLog(@"dealloc");
    > [speechSynth release];
    > [super dealloc];
    > }
    > @end
    >
    >
    > However when I run the application and quit it, I never see the text "dealloc" appear in the log window. Does this mean that this method is never called?

    Correct. There is no point individually freeing the memory for every
    object at application quit time, because the OS will automatically
    reclaim all the memory in one big chunk.

    If you want to know when your application is quitting, you should use
    the applicationWillTerminate: delegate method or
    NSApplicationWillTerminate notification.

    -- Finlay
  • On Sep 29, 2007, at 8:38 AM, Bob Ueland wrote:

    > In Hillegass book chapter 4, there is a class called AppController
    > instantiated with IB. In the file AppController.m there is the
    > following method:
    >
    > - (void)dealloc
    > {
    > NSLog(@"dealloc");
    > [speechSynth release];
    > [super dealloc];
    > }
    > @end
    >
    >
    > However when I run the application and quit it, I never see the
    > text "dealloc" appear in the log window. Does this mean that this
    > method is never called?

    Hey Bob,

    At first glance, the memory responsibility of Nib objects can be
    confusing, but here are some good explanations from the ADC:

    "The File’s Owner of an application’s main nib file (which contains
    the application menu and possibly other items) is the global
    application object NSApp. However, when a Cocoa application
    terminates, top level objects in the main nib do not automatically
    get dealloc messages just because NSApp is being deallocated. In
    other words, even in the main nib file, you have to manage the memory
    of top-level objects."

    "With single-window applications, objects in the main nib file
    persist through the runtime life of the application and are released
    when the application terminates; however, dealloc is not guaranteed
    to be automatically invoked on objects from the main nib file when an
    application terminates."

    If you need more fine-grained control about the disposal of your
    Nib's top level objects (especially if you're loading multiple
    instances of a given nib and associated controller objects or you
    specifically require that dealloc be executed), look into using
    NSWindowController to manage everything for you.

    See 'Memory Management of Nib Objects' for more information: <http://
    developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
    Articles/MemMgmtNibObjects.html#//apple_ref/doc/uid/TP40004998>

    - Sean
  • On Sep 29, 2007, at 5:46 AM, Finlay Dobbie wrote:

    > On 29/09/2007, Bob Ueland <bobueland...> wrote:
    >> In Hillegass book chapter 4, there is a class called AppController
    >> instantiated with IB. In the file AppController.m there is the
    >> following method:
    >>
    >> - (void)dealloc
    >> {
    >> NSLog(@"dealloc");
    >> [speechSynth release];
    >> [super dealloc];
    >> }
    >> @end
    >>
    >>
    >> However when I run the application and quit it, I never see the
    >> text "dealloc" appear in the log window. Does this mean that this
    >> method is never called?
    >
    > Correct. There is no point individually freeing the memory for every
    > object at application quit time, because the OS will automatically
    > reclaim all the memory in one big chunk.

    That's true, of course, but the counterpoint is that dealloc can be
    used for more than just strictly freeing memory, and these behaviors
    will be lost at shutdown time.
    For C++ veterans, this can be a rough lesson, because in C++, app
    shutdown does invoke destructors, and it's very common for a
    destructor to do much more than simply freeing malloc'ed blocks.
    Of course, in ObjC, using dealloc to do interesting things will be
    frowned on even more in the near future, as garbage collection makes
    it impossible to know if or when your dealloc will be called. Could
    be immediately, could be much later, could be never! That's fine—all
    garbage collection systems are like this—but it does impose another
    limitation on what you can reasonably do in a dealloc.

    > If you want to know when your application is quitting, you should use
    > the applicationWillTerminate: delegate method or
    > NSApplicationWillTerminate notification.

    And then it's your job to manually release the objects you care about
    getting dealloc called on, or do other special behaviors to get your
    cleanup code to run.
  • On Sep 29, 2007, at 7:56 AM, John Stiles wrote:
    > On Sep 29, 2007, at 5:46 AM, Finlay Dobbie wrote:
    >> On 29/09/2007, Bob Ueland <bobueland...> wrote:
    >>> In Hillegass book chapter 4, there is a class called
    >>> AppController instantiated with IB. In the file AppController.m
    >>> there is the following method:
    >>>
    >>> - (void)dealloc
    >>> {
    >>> NSLog(@"dealloc");
    >>> [speechSynth release];
    >>> [super dealloc];
    >>> }
    >>> @end
    >>>
    >>>
    >>> However when I run the application and quit it, I never see the
    >>> text "dealloc" appear in the log window. Does this mean that this
    >>> method is never called?
    >>
    >> Correct. There is no point individually freeing the memory for every
    >> object at application quit time, because the OS will automatically
    >> reclaim all the memory in one big chunk.
    > That's true, of course, but the counterpoint is that dealloc can be
    > used for more than just strictly freeing memory, and these
    > behaviors will be lost at shutdown time.

    Yes -- it can be used for more than just freeing memory.

    Don't do that.

    Overloading the Cocoa reference counting mechanism to manage more
    than memory leads to all kinds of hard to debug problems and
    instabilities.  And it simply doesn't work under a garbage collected
    Objective-C, coming in Leopard.

    From the archives:

    http://lists.apple.com/archives/cocoa-dev/2007/Jul/msg01220.html

    b.bum
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