I am having a small method returning the first week-day of a given date:
- (NSDate*) getFirstDayOfTheWeekFor:(NSDate*)date {
NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDayDate; //This is 100.0% leaking acc. to the Performance Tool Leaks
unsigned yearAndWeek = NSYearCalendarUnit | NSWeekCalendarUnit;
// retrieve the components from the current date
NSDateComponents *compsCurrentDate = [[gregorianCalender components:yearAndWeek fromDate:date] autorelease];
[compsCurrentDate setWeekday:2]; // Monday
[compsCurrentDate setHour:0];
[compsCurrentDate setMinute:0];
[compsCurrentDate setSecond:0];
// make a date from the modfied components
firstDayDate = [[gregorianCalender dateFromComponents:compsCurrentDate] autorelease];
return firstDayDate;
}
As you can see, I am already trying to autorelease every single variable that is used here (this is not what it looked like before I started to track down the leaks). Originally I wanted to explicitly release all variables before the return, except the "firstDayDate" variable, which HAS to be autoreleased because of the return.
These are the Leaked Objects found by the Performance Tool:
The mistake MUST be something completely stupid but I can't find it. Can you help me? THANK YOU!!
It should look like this:
- (NSDate*) getFirstDayOfTheWeekFor:(NSDate*)date
{
NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDayDate; //This is 100.0% leaking acc. to the Performance Tool Leaks
unsigned yearAndWeek = NSYearCalendarUnit | NSWeekCalendarUnit;
// retrieve the components from the current date
NSDateComponents *compsCurrentDate = [gregorianCalender components:yearAndWeek fromDate:date];
[compsCurrentDate setWeekday:2]; // Monday
[compsCurrentDate setHour:0];
[compsCurrentDate setMinute:0];
[compsCurrentDate setSecond:0];
// make a date from the modfied components
firstDayDate = [gregorianCalender dateFromComponents:compsCurrentDate];
return firstDayDate;
}
If firstDayDate is leaking, it's not in this method. Check downstream. Also, the icu part looks a little fishy to me. It could be a bug/leak in iOS wrapper around the icu library.
Remember you only release
or autorelease
if you alloc, init
, or copy
.