Cocoa-Touch: NSDateComponents crashes on releasing a fresh instance
-
20-09-2019 - |
Question
I have:
NSDate *d = [[NSDate alloc] init];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dc = [calendar components:unitFlags fromDate:d];
// doing something with dc.day, dc.month, dc.year
[dc release];
[d release];
I've tried removing the "doing something.. part", just did a get and release, and it still crashes. If I just comment out the [dc release] part, it all works fine.
I'm new to Obj-C. Any idea why this crashes? Am I doing something wrong?
Solution
You shouldn't be releasing dc
. It does not come from an alloc, new, or copy.
Brush up on the Cocoa Memory Management Rules
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow