• 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: mmalcolm crawford <email@hidden>
  • Date: Fri, 25 Nov 2005 20:17:20 -0800


On Nov 25, 2005, at 7:50 PM, David Gimeno Gost wrote:

It follows the memory management rules, well may be said that is allows callers to follow the memory management rules without caring that the object is really a singleton. It just happens in the implementation of the methods used that it results in nothing being done.

But you don't need to override the methods to accomplish that. What I'm saying is that the default (inherited) methods already do the right job if users of the class follow the Cocoa's memory management rules.

This is not the case. If you didn't override release, then (following Cocoa's memory management rules):


MyGizmoClass *gizmo = [[MyGizmoClass alloc] init];
// do some stuff
[gizmo release];

// do some more stuff

MyGizmoClass *gizmo2 = [[MyGizmoClass alloc] init];
[gizmo2 doSomething];  // crash

At this point, sharedGizmoManager is a pointer to a freed object.


2. The -retain and -release methods shouldn't be overridden. There is no point in doing so and the way it is done in the current sample code guarantees that code breaking Cocoa's memory management rules will go by unnoticed.

The whole point of a singleton object is that, if it is created, it remains valid for the remainder of the lifetime of the application. If it were possible to release it, then it could be deallocated, defeating the purpose (see also above)...

Overriding the retain and (in particular) release methods means that (as Shawn suggested) a developer can continue to adhere to the normal rules of memory management without needing to "special case" any objects. This typically simplifies things.

mmalc

_______________________________________________
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


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

  • Prev by Date: Subversion and XCode
  • Next by Date: Re: Subversion and XCode
  • 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