• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: There's obviously something I don't understand about NSDate.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: There's obviously something I don't understand about NSDate.


  • Subject: Re: There's obviously something I don't understand about NSDate.
  • From: Dave Fernandes <email@hidden>
  • Date: Sat, 17 Mar 2012 18:57:29 -0400

Yes, [NSDate date] returns an autoreleased object. So you need to retain it if you want to use it later. Once you are done with it, you should release it. See:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-SW6

If a Cocoa method name doesn't begin with “alloc”, “new”, “copy”, or “mutableCopy”, then the returned object is autoreleased.

Dave

On 2012-03-17, at 6:38 PM, G S wrote:

> 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!
>
> Gavin
> _______________________________________________
>
> 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


_______________________________________________

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


  • Follow-Ups:
    • Re: There's obviously something I don't understand about NSDate.
      • From: G S <email@hidden>
    • Re: There's obviously something I don't understand about NSDate.
      • From: Dave Fernandes <email@hidden>
References: 
 >There's obviously something I don't understand about NSDate. (From: G S <email@hidden>)

  • Prev by Date: There's obviously something I don't understand about NSDate.
  • Next by Date: Re: There's obviously something I don't understand about NSDate.
  • Previous by thread: There's obviously something I don't understand about NSDate.
  • Next by thread: Re: There's obviously something I don't understand about NSDate.
  • Index(es):
    • Date
    • Thread