Bug 7766 – (Full Closures) Chain of nested functions fails
Status
RESOLVED
Resolution
DUPLICATE
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2012-03-24T19:43:00Z
Last change time
2012-03-27T23:23:02Z
Keywords
wrong-code
Assigned to
nobody
Creator
xinok
Comments
Comment #0 by xinok — 2012-03-24T19:43:02Z
In the following code, you'd expect it to print 365. However, it prints a random number instead. The issue is that 'value' is referred to by 'foo', which is referred to by 'foo2'. DMD fails to detect this chain, so 'value' is allocated on the stack, not the heap. Once the function 'clbug' returns, 'value' is popped from the stack and so it ends up printing a random number.
value -> foo -> foo2
void main(){
clbug()();
}
int delegate() clbug(){
int value = 365;
int foo(){
writeln(value);
return value;
}
int foo2(){
return foo();
}
return &foo2;
}
Comment #1 by clugdbug — 2012-03-27T23:23:02Z
Duplicate of bug 1841 - it's incorrect closure detection in nested functions.
*** This issue has been marked as a duplicate of issue 1841 ***