我必须在下面的代码中释放 NSDate 吗?
我必须在下面的代码中释放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必释放
-[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 referencedcomponents
(an instance ofNSDateComponents
, 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?
不,你不必释放它。它是自动发布的。
No you don't have to release it. It is autoreleased.