[英]Memory leak with singleton
我有一个单例类,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方法来释放在那里创建的对象。 为了完整起见,您可能应该拥有它,但是只要该类仅作为一个单例使用,那实际上并不重要。
这不是泄漏。 通常,单例会永远存在,因此应该保留在内存中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.