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: John Mikros <email@hidden>
- Date: Wed, 22 Sep 2004 12:29:37 -0700
On Sep 22, 2004, at 11:20 AM, James Bucanek wrote:
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...
While it's possible to code what you're doing in a thread safe manner,
it's just easier to not have to worry about the object being released
at a (somewhat) random moment with respect to the other thread.
If you must use an autorelease pool, I would do something like:
NSAutoreleasePool* auto = [NSAutoreleasePool alloc] init];
Buffer* myBuffer = [BufferFactory buffer];
bool sendToThread = ![self determineIfThereIsAnError:myBuffer];
if (sendToThread)
[myBuffer retain];
[auto release];
if (sendToThread)
{
[myBuffer sendToOtherThread];
}
_______________________________________________
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