NSDateComponents/NSCalendar leak
If I analyze my program with the XCode 4 tool, the following line leads to "Potential leak of an object allocated on line 127".
NSDateComponents *weekdayComponents = [[开发者_如何学Python[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] components:NSWeekdayCalendarUnit fromDate:[[NSDate date] dateByAddingTimeInterval:(60*60*24)]];
if I try to release it ([weekdayComponents release];
), a warning reading "Incorrect decrement of the reference count of an object that is not owned at this point by the caller" appears. Any ideas?
Thanks in advance!
The problem in your code is that you don't hang on to the NSCalendar object, so you can't release it anymore.
In other words, you are leaking the NSCalendar object. If you insist on having everything in one statement, you should change it to:
NSDateComponents *weekdayComponents =
[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]
components:NSWeekdayCalendarUnit
fromDate:[[NSDate date] dateByAddingTimeInterval:(60*60*24)]
];
Even better is to split the statement:
NSCalendar *calendar =
[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[calendar
components:NSWeekdayCalendarUnit
fromDate:[[NSDate date] dateByAddingTimeInterval:(60*60*24)]
];
[calendar release];
精彩评论