开发者

Leak? dateFromComponents surely returns autoreleased object, no?

开发者 https://www.devze.com 2023-03-17 07:04 出处:网络
-(NSDate *)dateFromDate:(NSDate *)inDate withNewTime:(NSDateComponents *)inTimeComponents { NSCalendarUnit u开发者_开发知识库nitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
-(NSDate *)dateFromDate:(NSDate *)inDate withNewTime:(NSDateComponents *)inTimeComponents {

NSCalendarUnit u开发者_开发知识库nitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *newComponents = [self.sysCalendar components:unitFlags fromDate:inDate];
[newComponents setHour:[inTimeComponents hour]];
[newComponents setMinute:[inTimeComponents minute]];
return [self.sysCalendar dateFromComponents:newComponents];

}

I can't figure why this would leak. Would this not return an autoreased object? It works as intended, but when I run on instruments, it appears that a date object is leaking and the stack indicates it is created here.

sysCalendar is a property with the following getter:

-(NSCalendar *)sysCalendar {
if (sysCalendar != nil) {
    return sysCalendar;
}

self.sysCalendar = [NSCalendar currentCalendar];
return sysCalendar;

}


When Instruments shows you where a leak occurs, it is actually showing you where the leaked object is created.

In this case it seems that the object created here is not being properly released elsewhere.


Just to be clear: you're saying an NSDate object is leaking, not an NSDateComponents object, right?

So Leaks can't generally tell you where you may have forgotten to release the date that was created here in this return statement. It's just telling you this is where it was created.

So look to see what you do with this new NSDate after you get it back from this method.

0

精彩评论

暂无评论...
验证码 换一张
取 消