【发布时间】:2010-01-29 01:29:18
【问题描述】:
这里是代码
+ (SalesCollection*)sharedCollection {
@synchronized(self) {
if (sharedInstance == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release {
/* Problem in Here */
[myDict release];
sharedInstance = nil;
[sharedInstance release];
}
- (id)autorelease {
return self;
}
// setup the data collection
- init {
if (self = [super init]) {
[self setupData];
}
return self;
}
这里是我的 .h 文件
@interface MyCollection : NSObject {
NSMutableDictionary *myDict;
}
@property (nonatomic,retain) NSMutableDictionary * myDict;
+ (MyCollection*)sharedInstance ;
- (void)setupData;
我有一个 NSMutableDictionary (myDict) 包含对象数组。 现在我的问题是我想在按钮单击时刷新这些数据。所以我在 - (void)release 方法中释放此实例,然后再次尝试 Init 但这会造成大量泄漏,因为它可能不会从 myDict 中释放对象数组 那么如何实现这一点。我按照苹果的相同示例 "TheElement" 来创建单例。
谢谢
【问题讨论】:
标签: iphone objective-c performance memory-leaks singleton