Bug 15195 – DIP25: Member functions can leak local variables

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-10-12T13:34:00Z
Last change time
2015-10-21T22:59:53Z
Keywords
performance
Assigned to
nobody
Creator
timon.gehr

Comments

Comment #0 by timon.gehr — 2015-10-12T13:34:26Z
The following code leaks a reference to a local variable: ref int foo()@safe{ int x; struct S{ ref int bar(){ return x; } } S s; return s.bar(); } It is not enough to require 'return' on member functions if the returned reference is not guaranteed to outlive the 'this' pointer. (This is closely related to issue 15192.)
Comment #1 by timon.gehr — 2015-10-12T21:46:27Z
The compiler actually allocates a closure (so unlike the other DIP25 bug reports, there is no memory corruption here.) I'm not sure what the intended behaviour is. The closure is allocated even in the following case: void foo()@nogc{ int x; struct S{ int bar()@nogc{ return x; } } S s; } Error: function foo @nogc function allocates a closure with the GC
Comment #2 by bugzilla — 2015-10-21T22:49:22Z
Passing the address of a local variable to a nested function causes a closure to be allocated. It's working as designed, though the analysis is unsophisticated and could potentially be improved, so marked as an enhancement request.
Comment #3 by timon.gehr — 2015-10-21T22:59:53Z
(In reply to Walter Bright from comment #2) > Passing the address of a local variable to a nested function causes a > closure to be allocated. It's working as designed, though the analysis is > unsophisticated and could potentially be improved, so marked as an > enhancement request. I have opened https://issues.dlang.org/show_bug.cgi?id=15232. I'm closing this issue.