I am a complete newcomer to Object C but I thought I had the idea of memory management at least fundamentally correct. I have this simple method:
-(int)weekIdFromDate:(NSDate *)inDate {
NSCalendar *currentCal = [NSCalendar currentCalendar];
NSDateComponents *component = [currentCal components:NSWeekCalendarUnit fromDate:inDate];
int week = [component week];
//I don't understand why this fails, it seems correct to release these objects now that they
// are no longer needed
[currentCal release];
[component release];
return week;
}
The method exists in my root view controller, but the warning is in the “main” function. I used a zombie to locate the cause of “BAD_ACCESS…”. All works fine with no issues if I comment these to releases out.
You are releasing objects that you don’t own.
The methods you use to get
currentCalandcomponentreturnautoreleasedobjects, which are not your responsibility torelease.You only call release on objects that you get with
newallocorcopy, or objects that you send aretainmessage. (Remember the mnemonic NARC).All explained in the Memory Management Guide