Re: Protecting singleton objects from releasing
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.