• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is Apple's singleton sample code correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Apple's singleton sample code correct?


  • Subject: Re: Is Apple's singleton sample code correct?
  • From: glenn andreas <email@hidden>
  • Date: Tue, 29 Nov 2005 09:49:01 -0600


On Nov 29, 2005, at 9:02 AM, Darkshadow wrote:


On Nov 29, 2005, at 7:11 AM, Serge Cohen wrote:


There is nothing wrong about _using_ object deallocation to do the app-quit cleanup.

There's all wrong with it. I did it, long ago. I've learnt the hard way.



Can you be more precise on this one? (avoiding us to learn the hard way).



Serge.


-dealloc is not guaranteed to be called when the application is quitting. So if you have resources (other than memory) that need to be cleaned up, you should really listen for the - applicationWillTerminate: notification and do the clean up there.

This one bit me when I was first starting to learn Cocoa - I had a temp file written to disk that I wanted removed when the application quit. I put the removal in the -dealloc method. It took me a while to figure out that the file wasn't being removed because -dealloc wasn't being called (on this particular object) when the application quit.


I don't see this as such a big surprise - dealloc happens when the last reference to an object is made is removed (i.e., "release" is called). So when you quit the application, what is releasing that last reference? You expect something to magically go through all global variables, determine if they are pointing to something that responds to "release" (and hasn't already been released) and send that message? Remember, Cocoa is a library, and "release" (and the rest of that memory management architecture) is just part of that library (and not some language specified behavior that alters the semantics of your application like C++). And what if you're using another library that uses something other than "release" - would something magically fire off things as well?


It only makes sense, then, that the object responsible for handling the "terminate:" message (NSApp) provides a mechanism for others to be aware of it - which is exactly how it works. Again - the "dealloc" message has nothing to do with the system's tear down of a process (nor anything in the Objective-C runtime) - it is just a message sent to an object, no magic. A singleton doesn't get a dealloc message because nothing has sent it a release message that lowers it's retain count to zero.

And if the program crashes, or is "force quit", it doesn't matter what you do, no clean up will happen (not even via a C++ destructor).

Glenn Andreas                      email@hidden
 <http://www.gandreas.com/> wicked fun!
Widgetarium | the quickest path to widgets

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Is Apple's singleton sample code correct?
      • From: Scott Ellsworth <email@hidden>
References: 
 >Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Shawn Erickson <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: mmalcolm crawford <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: mmalcolm crawford <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Christian Brunschen <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: glenn andreas <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Ondra Cada <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Ondra Cada <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Serge Cohen <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Darkshadow <email@hidden>)

  • Prev by Date: Re: timers and event loops
  • Next by Date: Core data accessor and KVC
  • Previous by thread: Re: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread