• 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: Sat, 3 Dec 2005 14:01:38 +0100

I forgot the call to super in -dealloc, sorry:

On 3 Dec 2005, at 01:47, David Gimeno Gost wrote:

Let's suppose that, for whatever reasons, we want to modify our singleton such that it can safely be used like this:

[MySingleton releaseSharedInstance];

[...]

- (void) dealloc
{
sSharedInstance = nil;
}

Just for the record, here's the complete implementation of this variation (assuming there are no other mistakes, that is :-)):

@implementation MySingleton

static MySingleton* sSharedInstance = nil;
static BOOL sCanBeReleased = NO;

+ (MySingleton*) sharedInstance
{
@synchronized( self ) {
if ( sSharedInstance == nil ) {
// Note: calls -singletonInit instead of -init
sSharedInstance = [[MySingleton alloc] singletonInit];
sCanBeReleased = YES;
}
}
return sSharedInstance;
}

+ (void) releaseSharedInstance
{
@synchronized( self ) {
if ( sCanBeReleased ) {
[sSharedInstance release];
sCanBeReleased = NO;
}
}
}

- (id) init
{
// If we got here, the instance was created by alloc/init
// instead of +sharedInstance. Discard it and return the
// one created by +sharedInstance instead.
// Send a retain message to account for the
// call to alloc/init made by the client.
[self release];
self = [MySingleton sharedInstance];
return [self retain];
}

- (id) singletonInit
{
self = [super init];
if ( self != nil ) {
// Your singleton initialization goes here.
// This method will never be called more than once
// for the same object.
}
return self;
}

- (void) dealloc
{
// Your singleton deallocation code goes here.
// ...
sSharedInstance = nil;
[super dealloc];
}

@end

Regards.

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

  • Prev by Date: Replace Tab key for nextKeyView with something else?
  • Next by Date: Directory enumeration up to a certain depth
  • 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