我必须在下面的代码中发布NSDate吗? [英] do I have to release the NSDate in this code below?

查看:55
本文介绍了我必须在下面的代码中发布NSDate吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在下面的代码中发布NSDate吗?

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)

我问的原因是当我运行XCode Profiler并单击内存跳跃的点之一时,它使这部分代码(即下面所附代码的第一行)高亮显示-即我正在查看探查器中的泄漏块"表.

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

推荐答案

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

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?

这篇关于我必须在下面的代码中发布NSDate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆