Re: Newbie: Object life within a method scope
Re: Newbie: Object life within a method scope
- Subject: Re: Newbie: Object life within a method scope
- From: Kevin Viggers <email@hidden>
- Date: Wed, 23 Feb 2005 00:37:46 -0700
Hi Joe,
Have a read over the NSAutoreleasePool documentation as well. It might
be helpful for you to get a feel for how the mechanism works, so that
you see that there really isn't anything magically going on. The
important point in regard to your question is that (in your typical
mono-thread app) any actual work a NSAutoreleasePool does towards
releasing objects happens in the *same* thread of execution as your
method. Intuitively this means that even if your method receives as
argument some object slated for reclamation, you can be confident that
the object will not be de-allocated while your code controls the
thread.
It would seem that your Java conditioning is steering you towards the
idea that objects are reclaimed by a separate thread (akin to the GC
thread in Java) and that objects could potentially be pulled out from
under your code, but this is typically not the case. It is only when an
object is shared between threads that this becomes a true concern.
On 22-Feb-05, at 9:24 PM, Jiva DeVoe wrote:
I think the important thing to understand here is that objects
received back from other methods should be assumed to be
"autoreleased".
What this means is that they still have a retain count > 0, but they
are marked to be released later in the autorelease loop. Here's the
thing though, the autorelease loop runs as part of the internal
application event loop, which you will not hit until you exit your
current method (unless you have something like another thread running
which might cause the loop to run).
On Feb 22, 2005, at 2:59 PM, Joseph Feld wrote:
Hi all,
Trying to get a handle on the whole retain and release system (my day
job is
in the Java world and the GC has definitely spoiled me) and I'm
curious
about the life of an object within a method scope. I found the Memory
Management: Object Ownership and Disposal document on the Apple Dev
site and
it states: "A received object is normally guaranteed to remain valid
within
the method it was received in (exceptions include multithreaded
applications
and some Distributed Objects situations). That method may also safely
return
the object to its invoker," which would seem to answer my question.
However, two bullet points later it warns: "Use retain and
autorelease when
needed to prevent an object from being invalidated as a normal
side-effect
of a message," which has me a bit confused about what normal
side-effects of
a message might cause me to lose my object.
So I guess my question is, if I'm not interested in an object beyond
the
scope of the method receiving it is it still prudent to retain it to
ensure
it survives the scope, or is that unnecessary?
Thanks,
Joe
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
--
Jiva DeVoe
http://www.devoesquared.com
PowerCard - Intuitive Project Management Software for Mac OS X
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden