I am fairly new with Objective-C memory management and although I thought I understood it, I have a problem that I cannot manage to solve.
I have this property:
@property (nonatomic, retain) NSDate *dateDisplayed;
that I assign in my viewDidLoad with a custom method:
self.dateDisplayed = [self dbDateFormatToNsDate:@"15/11/2011"];
My dbDateFormatToNsDate method looks like this:
- (NSDate *) dbDateFormatToNsDate:(NSString *) date {
NSDateFormatter *d = [[NSDateFormatter alloc] init];
[d setDateFormat:@"dd/MM/yyyy"];
NSDate *toReturn = [d dateFromString:date];
[d release];
return toReturn;
}
So it returns an autoreleased object (if NSDate follows the convention). But when I get out from viewDidLoadin another function trying to read dateDisplayed:
[dateDisplayed isEqualToDate:[self dbDateFormatToNsDate:@"15/11/2011"]]
I get an NSZombie exception. Thanks for any help!
When assigning using self.property the property is retained because the setter methos is called but when just assigning without using
self.it isnt. Assuming of course that you haveretainin the propery definition of the .h file.You could
[d autorelease];instead. I might be totaly off on this, but thetoReturnNSDate might need to keep the formatter around even after youve released it, causing the bad access:Try: