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

时光怂恿深爱的人放手 提交于 2019-12-14 03:54:57

问题


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

回答1:


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?




回答2:


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



来源:https://stackoverflow.com/questions/6092482/do-i-have-to-release-the-nsdate-in-this-code-below

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!