D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7766 - (Full Closures) Chain of nested functions fails
Summary: (Full Closures) Chain of nested functions fails
Status: RESOLVED DUPLICATE of issue 1841
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P2 minor
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-03-24 19:43 UTC by Xinok
Modified: 2012-03-27 23:23 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Xinok 2012-03-24 19:43:02 UTC
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 Don 2012-03-27 23:23:02 UTC
Duplicate of bug 1841 - it's incorrect closure detection in nested functions.

*** This issue has been marked as a duplicate of issue 1841 ***