NSDateFormatter 内存泄漏
在 Instrument 的帮助下,我发现以下代码段存在内存泄漏。 仪器显示 NSDateFormatter 此处内存泄漏。
- (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;
}
谢谢
With the help of Instrument I found that the following 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些行更改
通过
这些行还选择了日期是保留类型的属性,因此不要对其直接调用保留。它增加了保留计数。
change these lines
by these lines
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: