Re: Stack-based C++ class to wrap NSAutoreleasePool
Re: Stack-based C++ class to wrap NSAutoreleasePool
- Subject: Re: Stack-based C++ class to wrap NSAutoreleasePool
- From: Clark Cox <email@hidden>
- Date: Mon, 16 Nov 2009 11:50:09 -0800
On Mon, Nov 16, 2009 at 11:28 AM, BJ Homer <email@hidden> wrote:
> Does the stack-based idiom allow returning an autoreleased object?
No. It does not. For the autoreleased object to survive the
destruction of the C++ object, it would have to be autoreleased after
it's destruction. Though, if you're planning on returning an
autoreleased object, you're already assuming that there is an
autorelease pool in place, in which case, there is no need to wrap the
local function in your own pool.
> I'd think you'd end up with code like this:
> - (id)arrayWithStuff {
> StackAutoreleasePool();
> NSArray *array = [NSArray arrayWithObjects:obj1, obj2, etc, nil];
> return array;
> }
> which would essentially translate into:
> - (id)arrayWithStuff {
> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
> NSArray *array = [NSArray arrayWithObjects:obj1, obj2, etc, nil];
> [pool release];
> return array; // array is dead
> }
> Is there some reason why this would not happen?
No, your interpretation is correct. You wouldn't want to wrap such a
method in an autorelease pool at all; whether or not that pool is tied
to the method scope is largely irrelevant at that point.
--
Clark S. Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden