[phobos] Returning Scoped Allocators From Functions

Steve Schveighoffer schveiguy at yahoo.com
Mon Jul 18 08:10:48 PDT 2011


>________________________________
>From: David Simcha <dsimcha at gmail.com>
>
>I'm working on retrofitting TempAlloc (renamed to RegionAllocator)to fit Andrei's proposed allocator interface.  One issue I've run into is, what happens when a scoped allocator is returned from a function?  For example:
>
>RegionAllocator fun() {
>    RegionAllocator alloc;
>    alloc.malloc(42);
>
>    RegionAllocator alloc2;
>    alloc2.malloc(42);
>    return alloc2;
>}
>
>Now, alloc has died and alloc2 is still alive, violating LIFO requirements.  Since it's virtually impossible to do by mistake, I'm inclined to just make returning a RegionAllocator from a function after another one has been subsequently created undefined behavior.

Can RegionAllocator check on postblit (called during return? not sure) at least in debug mode whether it's valid or not.  I think this might solve the problem.

-Steve



More information about the phobos mailing list