i am trying to create a custom setter method for a nsdateformatter. I am not sure whether this is leak free and optimized. is the memory management done properly. I am seeing a leak here. I don’t know why . . .
@property (nonatomic, retain) NSDateFormatter *customDateFormatter;
...
@synthesize customDateFormatter;
..
- (NSDateFormatter *)customDateFormatter
{
if (customDateFormatter == nil)
{
[self setCustomDateFormatter:[[NSDateFormatter alloc] init]];//it leaks here :(
[self.customDateFormatter setLocale: self.locale];
[self.customDateFormatter setDateFormat:@"h:mm:ss"];
}
return self.customDateFormatter;
}
-(void) dealloc
{
[customerDateFormatter release];
self.customDateFormatter = nil;
[super dealloc];
}
There is an extra retain. Since your setter retains, and the alloc is a +1. Here’s the remedy: