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: James Bucanek <email@hidden>
- Date: Wed, 22 Sep 2004 11:20:35 -0700
John Mikros wrote on Wednesday, September 22, 2004:
>If these buffer objects are being passed between threads then I'd
>imagine that you'd want to avoid autorelease like the plague.
Actually, the autorelease was very useful. (see below)
I can see the obvious problem of returning an autoreleased object to another thread and having that thread assume the objects validity based solely on being autoreleased.
However, unless I'm missing something autorelease pools simply defer a release. So if one thread creates an autoreleased object and passes it to a client (in some safe manor[1]) and that client retains the object, then the object should be safe until the client releases or autoreleased the object itself.
Unless I'm missing something...
>You don't want the object autoreleased while you're still using it in
>the other thread.
As long as that other thread retains the object, what's the problem?
This was very useful for me because my factory object creates Buffer objects which get passed to client threads. In the ideal situation, the Buffer object comes back to the factory when it is done.
But in error conditions, the client might simply release it and not send it back to the factory. In this case, the autorelease pool of the original thread would take care of releasing the object later. Or, it might have already released by the original thread, in which case the client's release (or autorelease) would release it in the client's thread instead.
So unless an object has some thread-specific code in it's dealloc handler, I don't see that there should be any problem with creating and autoreleasing an object in one thread then retaining and releasing it in another.
Note: It occurs to me that NSObject's release handler is probably not thread safe. If that's the case, then I might overload 'release' to prevent two threads from releasing the object simultaneously.
James
[1] This would have to be done in some fashion that the object was retained before it left the context of the thread that created it.
--
James Bucanek <mailto: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