Understanding the D memory model re: Voldemort types

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 9 11:42:32 PDT 2015


On 4/9/15 2:23 PM, Matt Kline wrote:
> On Thursday, 9 April 2015 at 18:15:16 UTC, H. S. Teoh wrote:
>
>> The compiler detects when a variable is being closed over by a nested
>> function, and allocates them on the heap instead of the stack.
>
> Is there somewhere I can read more about this (besides the compiler
> source code)? What ramifications are there for local variables being
> closed over that have destructors? Does the Voldemort object then get
> implicit destructor code that destroys the closed-over variables when it
> falls out of scope?

It's called a closure. It's not specific to voldemort types, it's done 
whenever you take a delegate that needs the context pointer to the stack.

I believe destruction is performed on the stack frame data when the GC 
collects the stack frame.

See here: http://dlang.org/function.html#closures

In regards to voldemort types triggering a heap allocation of the stack 
frame, I can't find specific documentation on this, probably a good idea 
to add it.

-Steve


More information about the Digitalmars-d mailing list