我必须在下面的代码中释放 NSDate 吗?

发布于 2024-11-09 01:57:54 字数 814 浏览 3 评论 0原文

我必须在下面的代码中释放 NSDate 吗?

(即,或者它只是在方法中创建为局部变量,我不必担心)

我问的原因是当我运行 XCode Profiler 并单击内存跳跃的点之一时,它突出显示了这段代码(即下面所附代码中的第一行) - 即我正在查看探查器中的“泄漏块”表。

-(NSDate *) dateBySettingHour:(NSInteger)hour andMinute:(NSInteger)minute {

    // Get Calendar for Existing Date
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: self];

    // Set Hour and Minute
    [components setHour: hour];
    [components setMinute: minute];
    [components setSecond: 00];

    // Create resultant Date
    NSDate *newDate = [gregorian dateFromComponents: components];    // WHERE THE PROFILE HIGHLIGHTS

    // Clean Up
    [gregorian release];    

    return newDate;
}

do I have to release the NSDate in this code below?

(i.e. or is it the case that it's just created within a method as a local variable that I don't have to worry)

The reason I ask is when I run XCode Profiler and click on one of the points where memory is jumping up, it highligted this bit of code (i.e. the first line in the attached code below) - i.e. I'm looking at the "Leaks Blocks" table in the profiler..

-(NSDate *) dateBySettingHour:(NSInteger)hour andMinute:(NSInteger)minute {

    // Get Calendar for Existing Date
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: self];

    // Set Hour and Minute
    [components setHour: hour];
    [components setMinute: minute];
    [components setSecond: 00];

    // Create resultant Date
    NSDate *newDate = [gregorian dateFromComponents: components];    // WHERE THE PROFILE HIGHLIGHTS

    // Clean Up
    [gregorian release];    

    return newDate;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夜司空 2024-11-16 01:57:54

您不必释放 -[NSCalendar dateFromComponents:] 返回的 NSDate 对象。 我的猜测是,该行被突出显示,因为这是您上次引用 components(希望是 NSDateComponents 的一个实例)并且您忘记释放 that 对象。

你的代码没问题。当我运行静态分析器(而不是分析器)时,它没有报告任何错误。我不确定为什么探查器会报告泄漏——也许 Cocoa 框架中存在内部泄漏?

You do not have to release the NSDate object returned by -[NSCalendar dateFromComponents:]. My guess is that the line was highlighted as it was the last time you referenced components (an instance of NSDateComponents, hopefully) and you forgot to release that object.

Your code is fine. When I run the static analyser (rather than the profiler), it reports no errors. I am not sure why the profiler would report a leak -- perhaps there's an internal leak in the Cocoa framework?

人事已非 2024-11-16 01:57:54

不,你不必释放它。它是自动发布的。

No you don't have to release it. It is autoreleased.

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