NSDateFormatter 内存泄漏

发布于 2024-11-18 20:12:50 字数 916 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2024-11-25 20:12:50

这些行更改

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

通过

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

这些行还选择了日期是保留类型的属性,因此不要对其直接调用保留。它增加了保留计数。

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.

人心善变 2024-11-25 20:12:50

您不需要保留消息。它将由代表拥有。

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

薄凉少年不暖心 2024-11-25 20:12:50

试试这个:

- (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;
}

Try this:

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文