Reputation: 34818
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;
}
Upvotes: 2
Views: 339
Reputation: 11330
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?
Upvotes: 3