I’ve got a view controller that after leaving the stack, shows a memory leak in the Leaks instrument. After reading lots of posts about the NSDateFormatter bug, and implementing the setDateFormat ‘Z’ workaround, I’m still leaking memory (according to Instruments).
In my header:
NSDateFormatter *dfm;
...
@property (nonatomic, retain) NSDateFormatter *dfm;
In my implementation:
@synthesize dfm;
...
- (void) viewDidLoad {
[super viewDidLoad];
dfm = [[NSDateFormatter alloc] init];
[self.dfm setDateFormat:@"h:mma Z"]; // leaks with & without this line
}
...
- (void)viewDidUnload {
//SOLUTION: This method was never being called. Needed to use dealloc, per the answer below.
[dfm release];
self.dfm = nil;
[super viewDidUnload];
}
Anything stand out as incorrect? The only thing I do with dfm in this class is call stringFromDate in a few places to return autoreleased strings that I use with UILabels.
Thanks in advance.
You can’t rely on
viewDidUnloadto be called. You also need:You only need to
self.dfm = nilbecause the default synthesized setter will do the release.