Re: Will this leak memory?
Re: Will this leak memory?
- Subject: Re: Will this leak memory?
- From: Shaun Wexler <email@hidden>
- Date: Fri, 5 Sep 2003 12:05:28 -0700
On Sep 5, 2003, at 8:22 AM, publiclook wrote:
Because it is perfectly normal to add singleton instances to
collections and later remove them. Collections retain the instance
when it is added and release it when it is removed. It is perfectly
proper to send the release message to a singleton following the normal
reference counting semantics.
It is NOT proper to copy a singleton or send so many release messages
that dealloc is called prior to program termination, but it is NEVER
proper to misuse release.
Exactly.
For those interested, I wrote a thread-safe "hardcore" singleton
abstract class, which prevents any subclass instances from being
re-initialized, such as if they later received an improper +[[singleton
alloc] init].
Feel free to use the class. Please send me any suggestions or
improvements...
--
Shaun Wexler
MacFOH
http://www.macfoh.com
/
/-----------------------------------------------------------------------
// SKWSingletonObject.h
// Copyright (c) 2001 SKW Development. All rights reserved.
/
/-----------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
@interface SKWSingletonObject : NSObject {
}
+ (id)sharedInstance;
@end
/
/-----------------------------------------------------------------------
// SKWSingletonObject.m
// Copyright (c) 2001 SKW Development. All rights reserved.
/
/-----------------------------------------------------------------------
#import "SKWSingletonObject.h"
#import <objc/objc-runtime.h>
@implementation SKWSingletonObject
Method initClassMethod;
Method initProxyMethod;
NSLock *sharedInstancesLock;
NSMutableDictionary *sharedInstances;
+ (void)load
{
initClassMethod = class_getInstanceMethod(self, @selector(init));
initProxyMethod = class_getInstanceMethod(self,
@selector(cannotReinitializeSingleton));
sharedInstancesLock = [[NSLock alloc] init];
sharedInstances = [[NSMutableDictionary alloc] initWithCapacity:1];
}
+ (id)sharedInstance
{
Class class = [self class];
id singleton;
[sharedInstancesLock lock];
singleton = [sharedInstances objectForKey:class];
if (!singleton) {
singleton = [class_createInstanceFromZone(class, 0U, (NSZone
*)[(NSObject *)self zone]) init];
if (singleton) {
Method init = class_getInstanceMethod(class, @selector(init));
if (init && (init->method_imp != initClassMethod->method_imp)) {
init->method_imp = initProxyMethod->method_imp;
}
[sharedInstances setObject:singleton forKey:class];
}
}
[sharedInstancesLock unlock];
return singleton;
}
- (id)cannotReinitializeSingleton
{
//[self doesNotRecognizeSelector:@selector(init)];
return self;
}
+ (id)new
{
//[self doesNotRecognizeSelector:_cmd];
return [self sharedInstance];
}
+ (id)allocWithZone:(NSZone *)zone
{
//[self doesNotRecognizeSelector:_cmd];
return [self sharedInstance];
}
+ (id)alloc
{
//[self doesNotRecognizeSelector:_cmd];
return [self sharedInstance];
}
- (id)init
{
return [super init];
}
- (id)copy
{
//[self doesNotRecognizeSelector:_cmd];
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
//[self doesNotRecognizeSelector:_cmd];
return self;
}
- (id)mutableCopy
{
//[self doesNotRecognizeSelector:_cmd];
return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
//[self doesNotRecognizeSelector:_cmd];
return self;
}
- (unsigned)retainCount
{
return UINT_MAX;
}
- (oneway void)release
{
}
- (id)retain
{
return self;
}
- (id)autorelease
{
return self;
}
- (void)dealloc
{
//[self doesNotRecognizeSelector:_cmd];
}
@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.