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: mmalcolm crawford <email@hidden>
- Date: Wed, 22 Sep 2004 00:29:10 -0700
On Sep 21, 2004, at 11:58 PM, Scott Stevenson wrote:
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.
I can see good reason to want to release a large number of temporary
buffers that might be created during a single event loop, however it
still seems things are being made more complicated than they might...
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).
The naming convention would, I believe, be 'newBufferFromPool' (see
'new"
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSObject.html>):
- (Buffer*)newBufferFromPool
{
return ( [[Buffer alloc] initWithMagic] );
}
But it would be far better to create a situation where you can let
autorelease do its thing normally.
Far better still would be simply not to use autorelease (as you implied
above). Without additional context, it's not clear why something like:
Buffer *bufferFromPool = [Buffer bufferFromPool];
// ...
could not be replaced with:
Buffer *bufferFromPool = [Buffer newBufferFromPool];
// ...
[bufferFromPool release];
? No autorelease "issues," and more efficient.
If there's a good reason why init/release won't suit, then the usual
strategy is to create your own autorelease pools...
On Sep 21, 2004, at 8:31 PM, James Bucanek wrote:
I know I could create a new NSAutoreleasePool just for this one object
-- but gee, that seems like a lot of overhead just to release a
buffer.
It's not clear why this represents "a lot of overhead"? You're
creating a single object. If this does represent "a lot of overhead"
in the current context, it's not clear why you're concerned about
releasing the buffer? What profiling have you done?
mmalc
_______________________________________________
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