• 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: David Gimeno Gost <email@hidden>
  • Date: Tue, 29 Nov 2005 22:58:26 +0100

On 29 Nov 2005, at 16:02, Darkshadow wrote:

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

That doesn't mean you can't do your clean up sending a -release message instead of a -doCleanUp message. That Cocoa/Objective-C does not provide runtime support for automatic deallocation of objects doesn't mean you shouldn't use object deallocation to handle disposal of resources. It just means you must call -release explicitly, just as you should have to call another method if you put your clean up code there. You still get the encapsulation and decoupling benefits of the pattern.


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.

You weren't bitten by putting the code in -dealloc. You were bitten by making the wrong assumption that -dealloc would automagically be called.



And before someone jumps in and we start a pointless discussion all over again, please notice that I'm not saying that everyone should use object deallocation to do resource cleanup, I'm just saying that there is nothing wrong about doing things that way, that there may be good reasons to do things that way, and that there is no point in preventing -dealloc from ever being called. If you don't want to put your cleanup code in -dealloc, fine. Removing that arbitrary constraint from the singleton does not prevent anybody from doing what he/she wants.


_______________________________________________
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: Andreas Mayer <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: Is Apple's singleton sample code correct?
  • Next by Date: Re: timers and event loops
  • 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