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 08:04:04 -0700
Scott Stevenson wrote on Tuesday, September 21, 2004:
>> Example:
>>
>> I have a pool of buffers. One message in that buffer pool creates a
>> new buffer object. Like a good Cocoa programmer, I autorelease the
>> object before I return it.
>>
>> - (Buffer*)bufferFromPool
>> {
>> return ( [[[Buffer alloc] initWithMagic] autorelease] );
>> }
>>
>> The client code uses the buffer, but now I want to destroy it
>> immediately after I'm done with it. I can't release it because it's
>> already been autoreleased.
>
>Why do you need it released immediately? The fact that you're running
>into this at all suggests you're making things more complicated than
>they need to be.
In this particular case, these buffers are being created (from the Pool object) and handed out to several threads which fill them with data. These are eventually passed back to the original thread which assembles them and writes them to disk. The number of buffer objects can, in certain circumstances, become quite large (millions) and I wanted to release them as soon as they had been written. There are also concurrency issues as some threads will block if they pool of available buffers is exhaushed. Releasing the buffer immediately allows them to be returned to the pool immediately.
>If it's that important, just ignore the rule and don't return it
>autoreleased. It would be a good idea to make this clear in the method
>name (retainedBufferFromPool perhaps). But it would be far better to
>create a situation where you can let autorelease do its thing normally.
I think I'll end up doing something like that. I was really trying to do this while sticking to the Cocoa guidelines, but in this case I think the simplest solution is to return a retained object to the client and release it when I'm done with it. Since the whole retain/release cycle is encapsulated in the buffer pool object, there shouldn't be any confusion.
_______________________________________________
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