我有一个单例类,init 方法中的行导致内存泄漏,我不知道为什么......
这是我的实现
static timerController *sngTimer = nil;
@implementation timerController
@synthesize repeatingTimer;
@synthesize dateComp;
@synthesize bPause;
+(timerController *) singletonTimer
{
@synchronized(self){
if (sngTimer == nil )
{
sngTimer = [[timerController alloc]init];
}
}
return sngTimer;
}
-(id)init
{
self = [super init];
if (self != nil) {
dateComp = [[NSDateComponents alloc] init]; ///this line cause memory leak
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; ///this line cause memory leak
[dateComp setCalendar:gregorianCalendar]; ///this line cause memory leak
[gregorianCalendar release];
bPause = FALSE;
}
return self;
}
最佳答案
我不知道你是如何检测泄漏的,但很可能你使用的任何工具都会注意到你没有 dealloc 方法来释放你在那里创建的对象。为了完整性,您可能应该拥有它,但只要该类仅用作单例,那么它并不重要。
关于iphone - 单例内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4158515/