NSDate & Memory management

Posted by iFloh on Stack Overflow See other posts from Stack Overflow or by iFloh
Published on 2010-05-19T05:12:46Z Indexed on 2010/05/19 5:20 UTC
Read the original article Hit count: 290

Filed under:
|

Hi, memory management still gives me grief. This time it is an NSDate iVar that I init using

NSDate *myNSDate = [[NSDate date] firstDayOfMonth];

with a method call to

- (NSDate *)firstDayOfMonth {
    NSDateComponents *tmpDateComponents = [[NSCalendar currentCalendar]
                                            components:NSYearCalendarUnit | NSMonthCalendarUnit | NSEraCalendarUnit | NSWeekCalendarUnit | NSWeekdayOrdinalCalendarUnit
                                            fromDate:self];
    [tmpDateComponents setDay:1];
    [tmpDateComponents setHour:0];
    [tmpDateComponents setMinute:0];
    [tmpDateComponents setSecond:0];
    return [[NSCalendar currentCalendar] dateFromComponents:tmpDateComponents];
}

At the end of the init call the retain count is at 1 (Note the iVar is not defined as a property).

When I step into the viewWillAppear method the myNSDate has vanished. I tried to do an explicit retain on it, but that only lasts until I update the iVar using the above method again.

I though - ok - I add the retain to the return of the function, but that makes the leak analyser throw up an error.

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone