Running dtor at the end of scope on allocated by closure struct
Maxim Fomin
maxim at maxim-fomin.ru
Wed Jan 2 11:37:43 PST 2013
Consider this:
import std.stdio;
struct S
{
int[] data;
this(this) { writeln("postblit"); data = data.dup; }
~this() { writeln("dtor"); data = null; }
}
auto foo()
{
S s = S([0]);
return { assert(s.data !is null); } ;
}
void main()
{
auto dg = foo();
dg();
}
Currently dmd inserts call to struct dtor at the end of foo
scope, as it does in most cases. However, struct object in this
case is still alive and running destructors on structs that would
exists far longer than usual scoped struct is not very good.
Moreover, the struct object is not copied, so that it becomes
"logically" invalid once it reaches end of function scope.
I argue for doing copy in this case. I am interested in what
decision was made for this situation and reasons behind it.
More information about the Digitalmars-d
mailing list