I have a singleton class and the lines in the init method cause memory leak, and I don’t kwno why…
this is my implementation
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;
}
I don’t know how you’re detecting the leak, but most likely whatever tool you’re using notices that you don’t have a dealloc method to release the object you create there. You probably should have it for completeness, but as long as the class is only used as a singleton, it doesn’t really matter.