• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Stack-based C++ class to wrap NSAutoreleasePool
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Stack-based C++ class to wrap NSAutoreleasePool
      • From: Ken Thomases <email@hidden>
References: 
 >Stack-based C++ class to wrap NSAutoreleasePool (From: Dan Korn <email@hidden>)
 >Re: Stack-based C++ class to wrap NSAutoreleasePool (From: Clark Cox <email@hidden>)
 >Re: Stack-based C++ class to wrap NSAutoreleasePool (From: Kyle Sluder <email@hidden>)
 >Re: Stack-based C++ class to wrap NSAutoreleasePool (From: Clark Cox <email@hidden>)
 >Re: Stack-based C++ class to wrap NSAutoreleasePool (From: BJ Homer <email@hidden>)

  • Prev by Date: Re: opening a document via Finder without NSDocument SOLVED
  • Next by Date: Re: NSUndoManager and runModalForWindow: (again)
  • Previous by thread: Re: Stack-based C++ class to wrap NSAutoreleasePool
  • Next by thread: Re: Stack-based C++ class to wrap NSAutoreleasePool
  • Index(es):
    • Date
    • Thread