Professional Web Applications Themes

SpeakLine example not deallocating - Mac Programming

Hello, I'm currently learning Cocoa programming using Aaron Hillegass's "Cocoa Programming For Mac OS X" (2nd edition), and I'm having some problems with the following: When building and running the "SpeakLine" example in Chapter 4, the application runs fine, but doesn't appear to call "dealloc" method upon quitting. Following the example in the book, I should get an NSLog message "dealloc" in my console window. But the application quit with status 0 without showing the message. I've followed the example word for word in my code, am wondering if I missed something? Any advice would be appreciated. Thank you. N....

  1. #1

    Default SpeakLine example not deallocating

    Hello,

    I'm currently learning Cocoa programming using Aaron Hillegass's
    "Cocoa Programming For Mac OS X" (2nd edition), and I'm having some
    problems with the following:

    When building and running the "SpeakLine" example in Chapter 4, the
    application runs fine, but doesn't appear to call "dealloc" method
    upon quitting. Following the example in the book, I should get an
    NSLog message "dealloc" in my console window. But the application quit
    with status 0 without showing the message.

    I've followed the example word for word in my code, am wondering if I
    missed something?
    Any advice would be appreciated. Thank you.

    N.
    Nicholas Guest

  2. #2

    Default Re: SpeakLine example not deallocating

    On Sun, 6 Jun 2004, Nicholas Tamara wrote:
     

    Normally, when an application quits, it doesn't bother to deallocate all
    of the objects contained. The operating system will clean up all of the
    memory used by the application regardless, so there's no need to take the
    time to do so in the program. I'm not familiar with the example you're
    using, but unless it listens for an application quit notification and
    releases things at that time, the behavior you're seeing is perfectly
    normal and acceptable.
    Michael Guest

  3. #3

    Default Re: SpeakLine example not deallocating

    Michael Ash <com> wrote in message news: 

    Thank you. The example shown in the book had us writing the following
    method:

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

    That's why I was expecting a message "dealloc" on the console, which
    didn't happen. Does it mean that dealloc may not be called everytime
    we quit an application, but relies on the operating system to
    "deallocate" objects? Under what cirstances would the program
    invoke dealloc then?

    I'm pretty new to programming, so forgive me if some of my questions
    seems silly. Thank you, greatly appreciate it.

    N.
    Nicholas Guest

  4. #4

    Default Re: SpeakLine example not deallocating

    On Sun, 6 Jun 2004, Nicholas Tamara wrote:
     

    The OS cleans up all memory used by an application when the app quits, so
    Cocoa often doesn't bother to clean stuff up when the app quits.

    This should not be taken to mean that you don't need to worry about memory
    management at all! For objects that aren't intended to last the entire
    life of the program, you must properly write -dealloc methods like the
    above, and properly release them when you're finished with them (if you
    need to).
    Michael Guest

  5. #5

    Default Re: SpeakLine example not deallocating

    On 2004-06-07 11:19:15 -0400, Michael Ash <com> said:
     

    Ok that makes no sense at all. If an object is created, the when the
    object is ready to be release, the dealloc should be called when the
    retain count goes to 0. What if my dealloc method has other code that
    doesn't involve just freeing memory. Example would be closing a file,
    or logging to a file.

    The code that the orginal poster is talking about is in the Controller
    for a simple application. Here is the init and dealloc.

    - (id) init
    {
    NSLog("AppController init");

    if( self = [super init] )
    {
    speechSynth = [ [NSSpeechSynthesizer alloc] initWithVoice:nil];
    [speechSynth setDelegate:self];
    }

    return self;
    }



    -(void) dealloc
    {
    NSLog("dealloc in AppController.");
    [speechSynth stopSpeaking];
    [speechSynth release];
    speechSynth = nil;
    [super dealloc];
    }


    Since I have alloced a new NSSpeechSythesizer object, I don't
    understand when it is destroyed. Does it have to do with the
    AppController being in the NIB file? If it is, why would I see the
    AppController object init NSLog, but not the NSLog for the dealloc.

    --
    J
    "If there is anything I can do for you or more to the point to you, let
    me know."

    J Guest

  6. #6

    Default Re: SpeakLine example not deallocating

    On Wed, 9 Jun 2004, J wrote:
     
    >
    > Ok that makes no sense at all. If an object is created, the when the object
    > is ready to be release, the dealloc should be called when the retain count
    > goes to 0. What if my dealloc method has other code that doesn't involve just
    > freeing memory. Example would be closing a file, or logging to a file.
    >
    > The code that the orginal poster is talking about is in the Controller for a
    > simple application. Here is the init and dealloc.
    >[/ref]
    [snip code] 

    There are two kinds of memory destruction, which is confusing you.

    As the app is running, you have the standard Cocoa-based refcounting. When
    an object's refcount drops to zero, it gets destroyed by sending the
    -dealloc message to the object. This will cause your log to print.

    When the app exits, the operating system cleans up its resources. The OS
    has no concept of your app's object model, and so it doesn't send -dealloc
    to any objects; it can't. It collects memory on the page level, not the
    object level.

    You asked, what if my -dealloc method does something like delete a file
    that the OS won't clean up? The answer is, don't do that. -dealloc is for
    cleaning up memory, nothing more. While you should certainly clean up
    files and such during -dealloc, you should not *depend* on -dealoc to do
    your cleanup. There is a notification that is sent before the app exits,
    and you should register for that if you need to clean something up at that
    time. And of course you should always take into account the possibility
    that your app will crash or otherwise abnormally terminate without getting
    the chance to do anything.
    Michael Guest

  7. #7

    Default Re: SpeakLine example not deallocating

    On 2004-06-10 06:19:28 -0400, Michael Ash <com> said:
     
    >>
    >> Ok that makes no sense at all. If an object is created, the when the
    >> object is ready to be release, the dealloc should be called when the
    >> retain count goes to 0. What if my dealloc method has other code that
    >> doesn't involve just freeing memory. Example would be closing a file,
    >> or logging to a file.
    >>
    >> The code that the orginal poster is talking about is in the Controller
    >> for a simple application. Here is the init and dealloc.
    >>[/ref]
    > [snip code] 
    >
    > There are two kinds of memory destruction, which is confusing you.
    >
    > As the app is running, you have the standard Cocoa-based refcounting.
    > When an object's refcount drops to zero, it gets destroyed by sending
    > the -dealloc message to the object. This will cause your log to print.
    >
    > When the app exits, the operating system cleans up its resources. The
    > OS has no concept of your app's object model, and so it doesn't send
    > -dealloc to any objects; it can't. It collects memory on the page
    > level, not the object level.
    >
    > You asked, what if my -dealloc method does something like delete a file
    > that the OS won't clean up? The answer is, don't do that. -dealloc is
    > for cleaning up memory, nothing more. While you should certainly clean
    > up files and such during -dealloc, you should not *depend* on -dealoc
    > to do your cleanup. There is a notification that is sent before the app
    > exits, and you should register for that if you need to clean something
    > up at that time. And of course you should always take into account the
    > possibility that your app will crash or otherwise abnormally terminate
    > without getting the chance to do anything.[/ref]

    OK that makes sense. I would think that the NSApplication that loads
    the NIB file, would release the Controller object that is contained in
    the NIB.

    Maybe I just don't understand NIB and Cocoa very well. I should read a
    little more of my book before spouting off.

    Thanks for the information.
    --
    J
    "If there is anything I can do for you or more to the point to you, let
    me know."

    J Guest

  8. #8

    Default Re: SpeakLine example not deallocating

    On Fri, 11 Jun 2004, J wrote:
     

    Normally a nib's owner is responsible for releasing all of the objects in
    the nib when it goes away. For example, NSWindowController handles this
    automatically for you.

    However, NSApplication just doesn't care. Think about it. When the app
    quits, why bother releasing anything? If NSApplication is going away, it's
    right before the app will exit and the OS will step in and tear everything
    down. There's no need to take the time to release all of the objects in
    your main nib in this case. It's a special case due to the lifetime of
    these particular objects, not the common case.
    Michael Guest

  9. #9

    Default Re: SpeakLine example not deallocating

    Michael Ash <com> wrote 

    Sorry if this is a stupid question, but how does the OS know which
    memory is being used by the application? Say, if I have created a
    couple of objects which are physically located in some memory address
    apart from the main app itself (could be created during compile time
    or runtime). When the app quits and the OS takes over to free up
    memory...how does it know where these objects are? Tia.

    My understanding of Computer Hardware and memory stuff is rudimentary,
    so please forgive if the question makes no sense.

    N.
    Nicholas Guest

  10. #10

    Default Re: SpeakLine example not deallocating

    On Fri, 11 Jun 2004, Nicholas Tamara wrote:
     
    >
    > Sorry if this is a stupid question, but how does the OS know which
    > memory is being used by the application? Say, if I have created a
    > couple of objects which are physically located in some memory address
    > apart from the main app itself (could be created during compile time
    > or runtime). When the app quits and the OS takes over to free up
    > memory...how does it know where these objects are? Tia.[/ref]

    Things like allocated memory, open file descriptors, sockets, etc. are
    tracked on a per-process basis. When a process dies, the OS goes through
    that process's list of memory, file descriptors, and so on and cleans them
    all up. This tracking is fundamental to the workings of a modern OS and
    really couldn't *not* be done. Imagine what would happen if the OS never
    cleaned up after an app; eventually you'd run out of memory if you never
    rebooted, and crashing/force quitting/killing an app would potentially
    leak hundreds of megabytes of unrecoverable memory.
    Michael Guest

Similar Threads

  1. deallocating?
    By Christopher J Bottaro in forum PERL Beginners
    Replies: 8
    Last Post: December 22nd, 05:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139