Reputation: 401
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
when i use [components release];
it crashes my application, how do i release components
when i run with leak instrument - my application crashes when it reaches usage of overall alloc 22MB
Upvotes: 0
Views: 152
Reputation: 723598
You must not release components
yourself at all as it's set to autorelease by the NSCalendar
object.
I suspect your leak is coming from your NSDate
object that you pass to that line of code. You should probably assign that to a local variable, pass it to the [cal components]
method then release the local variable:
NSDate *today = [[NSDate alloc] init];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:today];
[today release];
Or, set the use NSDate
object itself to autorelease as you pass it as you are doing,[NSDate date]
, which as David Gelhar commented will generate an autoreleased object, like so:
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:[NSDate date]];
Upvotes: 3
Reputation: 27900
The NSCalendar method components:fromDate:
returns an autoreleased object; you must not call release
on it yourself (unless you have first called retain
).
Check out the Memory Management Rules
Upvotes: 2