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: James Bucanek <email@hidden>
- Date: Sat, 17 Mar 2012 16:31:30 -0700
First, read the memory management link posted by Dave.
G S <mailto:email@hidden> wrote (Saturday, March 17,
2012 4:10 PM -0700):
Thanks, Dave. That's what I thought. But I don't understand why I need to
retain it then;
In general, you have to retain an object whenever you plan to
keep a reference beyond the scope of the current autorelease
pool. Any any object you retain, you're responsible for
releasing when you're done with it.
it's assigned to a member pointer. Why does it get
released, and when?
Because you kept the reference to it beyond the scope of the
current autorelease pool.
If I call retain on it, do I have to call release on
it later?
Yes.
I create another NSDate, on the stack, to hold "now" for use within that
function. Do I need to retain that too? If not, why not?
No, because the scope of automatic variables never extends
beyond the scope of the autorelease pool. So you're more or less
guaranteed of "forgetting" the reference before the current
autorelease pool is drained.
An autorelease pool essentially "owns" (retains) all of the
autoreleased objects you're using. If all of the references to
that object are within the lifetime of the autorelease pool
(typically one pass through the event loop for a Cocoa app),
then you can piggyback on the autorelease pool's ownership of
the object and generally ignore the whole retain/release thing,
since you'll be done with the object before the autorelease pool
releases it.
If, on the other hand, you want to keep a persistent reference
to the object, you must retain it and release it when you're done.
--
James Bucanek
_______________________________________________
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