Re: Newbie question: how to release an object that has been autoreleased
Re: Newbie question: how to release an object that has been autoreleased
- Subject: Re: Newbie question: how to release an object that has been autoreleased
- From: Ondra Cada <email@hidden>
- Date: Wed, 22 Sep 2004 20:48:23 +0200
Frederick,
On 22.9.2004, at 20:36, Frederick Cheung wrote:
If these buffer objects are being passed between threads then I'd
imagine that you'd want to avoid autorelease like the plague.
Nope. Why, on earth?
You don't want the object autoreleased while you're still using it
in the other thread.
Why? That does not make real sense. Autorelease is just a delayed
release.
If the object is used in another thread, it is bound to be retained
there (and eventually /auto/released there). Thus, no harm in in
being autoreleased in the first thread, for it won't be deallocated
(until it is /auto/released in all the threads in which it is used).
You don't know that the delayed release will happen after the other
thread has run and retained the object whereas in a non threaded world
you are usually sure that the delayed release occurs after whoever is
using the object has either finished with it or has retained it.
Wrong, you do know that. Or you don't, in which case you misuse
autorelease pools very badly :)
The trick is, autoreleasing in thread A just ensures the object lives
long enough to do whatever is needed, which *must* include vending it
to thread B. At which time B has to retain it (and perhaps autorelease
it if it is about to use it only "locally").
Did you check the thread-safe accessor code I've linked in the previous
message? It should be clear from it (in this case, from the getter
implementation).
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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