• 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: Protecting singleton objects from releasing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Protecting singleton objects from releasing


  • Subject: Re: Protecting singleton objects from releasing
  • From: petite_abeille <email@hidden>
  • Date: Sat, 15 Feb 2003 16:09:17 +0100

Hi Georg,

Is there any "recommended" way to protect singleton objects (e.g.
[NSProcessInfo processInfo]) from releasing? Overriding the release
method comes in mind, but I am wondering if there are better ways...

Well... I don't know if there is a "better way"... but I simply overwrite the release method...

Here is the Singleton superclass I use... it handles both deallocation (not allowed) and allocation (only once)...

@implementation Singleton

+ (id) sharedInstance
{
id aValue = [ClassVariable objectForKey: @"sharedInstance" withOwner: self];

if ( aValue == nil )
{
aValue = class_createInstanceFromZone ([self class], 0U, (NSZone*)[(NSObject*)self zone]);
[aValue init];
}

return aValue;
}

+ (id) alloc
{
return [self sharedInstance];
}

- (id) init
{
id sharedInstance = [ClassVariable objectForKey: @"sharedInstance" withOwner: [self class]];

if ( sharedInstance == nil )
{
sharedInstance = [super init];

[ClassVariable setObject: sharedInstance forKey: @"sharedInstance" withOwner: [self class]];
}

return sharedInstance;
}

- (id) copyWithZone: (NSZone *)aZone
{
[self doesNotRecognizeSelector:_cmd];

return nil;
}

- (void) dealloc
{
[self doesNotRecognizeSelector:_cmd];
}

- (id) autorelease
{
return self;
}

- (void) release
{
}

@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: movable status items?
  • Next by Date: Get path to user preference folder
  • Previous by thread: Re: Protecting singleton objects from releasing
  • Next by thread: Re: Protecting singleton objects from releasing
  • Index(es):
    • Date
    • Thread