[Issue 2043] Closure outer variables in nested blocks are not allocated/instantiated correctly.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jan 5 23:52:32 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=2043


Russ Lewis <webmaster at villagersonline.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |webmaster at villagersonline.c
                   |                            |om


--- Comment #8 from Russ Lewis <webmaster at villagersonline.com> 2013-01-05 23:52:25 PST ---
Comment 7's workaround is a little brilliant, though terribly hard to read. 
Basically, it's declaring a delegate with the
    () {
       <code>
    }
syntax, and calling it (with a trailing () ), all inline in each pass of the
for() loop.

This works around the problem because, as far as I can tell, the DMD compiler
will make only 1 heap copy of any given variable *per instance of a function
call*, even if the variable is a loop-local variable.  So, if you run 10
iterations of a for() loop, and create a delegate in each one, then all 10
iterations of the loop share the same heap space for their variables, even the
loop-local ones (which ought to have 10 different copies).  But, if you instead
call the same function 10 times in a row (as comment 7 effectively does), then
you get 10 different copies of the variable on the heap.

I can understand why this is a hard problem to solve on the compiler
side...but, IMHO, it's a fairly important one to solve.  This bug causes some
rather hard-to-understand bugs in what would otherwise look like
straightforward code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list