开发者

NSDateFormatter Memory leaks

With the help of Instrument I found that the foll开发者_JS百科owing section of the code leaking memory. Instrument saying NSDateFormatter leaking memory here.

- (NSDate*) dateSelected{
if(selectedDay < 1 || selectedPortion != 1) return nil;

TKDateInformation info = [monthDate dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
info.hour = 0;
info.minute = 0;
info.second = 0;
info.day = selectedDay;
NSDate *d = [NSDate dateFromDateInformation:info timeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];


HolidayAppDelegate *delegatObj = (HolidayAppDelegate *)[UIApplication sharedApplication].delegate;

NSDateFormatter  *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MMMM-dd-yyyy"];

    // below line getting leak
NSString *message = [[formatter stringFromDate:d] retain];
delegatObj.selecteddate=message;
NSLog(@" selectd %@ ",delegatObj.selecteddate);
[delegatObj getholiday_forcalnder];

return d;

}

Thanks


change these lines

NSString *message = [[formatter stringFromDate:d] retain];
delegatObj.selecteddate=message;

by these lines

 NSString *message = [formatter stringFromDate:d];
  [message retain];
delegatObj.selecteddate=message;
[message release];

also selected date is property which is of type retain so don't call directy retain on it. It increases the retain count.


You don't need the retain on message. It will be owned by the delegate.


Try this it may help you

- (NSDate*) dateSelected
{
if(selectedDay < 1 || selectedPortion != 1) return nil;

TKDateInformation info = [monthDate dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

info.hour = 0;

info.minute = 0;

info.second = 0;

info.day = selectedDay;

NSDate *d = [NSDate dateFromDateInformation:info timeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];


HolidayAppDelegate *delegatObj = (HolidayAppDelegate *)[UIApplication sharedApplication].delegate;

NSDateFormatter  *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"MMMM-dd-yyyy"];

NSString *message = [formatter stringFromDate:d];

delegatObj.selecteddate=message;

NSLog(@" selectd %@ ",delegatObj.selecteddate);

[delegatObj getholiday_forcalnder];

[formatter release];

return d;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜