Re: There's obviously something I don't understand about NSDate.
Re: There's obviously something I don't understand about NSDate.
- Subject: Re: There's obviously something I don't understand about NSDate.
- From: Matt Neuburg <email@hidden>
- Date: Mon, 19 Mar 2012 11:39:17 -0700
On Sat, 17 Mar 2012 15:38:20 -0700, G S <email@hidden> said:
>I have a member variable to hold an NSDate:
>
> NSDate* _firstBadAccuracyTime;
>
>At some point, something happens and I set this value to "now":
>
> _firstBadAccuracyTime = [NSDate date];
>
>On my next trip through this function, I calculate how long it has been
>since I set this date:
>
> NSDate* now = [NSDate date];
> if([now timeIntervalSinceDate:_firstBadAccuracyTime] >
>BAD_ACCURACY_TIME)
>
>then CRASH: BAD ACCESS
>
>_firstBadAccuracyTime still contains a valid address, but the object
>must've been released. Why? If I add a retain where I assign it, the
>crash doesn't happen. I added autorelease, but then got a crash on
>releasing an object I hadn't allocated (which makes me think it's already
>autoreleased).
>
>Thanks for any insight!
http://www.apeth.com/iOSBook/ch12.html#_memory_management
The mistake you're making here is *exactly* the mistake marked with the comment "in non-ARC code this would be a bad idea".
Of course ARC relieves you of such worries. And so does passing thru a property with a synthesized accessor, as that chapter goes on to explain.
m.
--
matt neuburg, phd = email@hidden, <http://www.apeth.net/matt/>
A fool + a tool + an autorelease pool = cool!
Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden