www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Running dtor at the end of scope on allocated by closure struct

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();

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.
Jan 02 2013