destructor order

Sean Kelly sean at invisibleduck.org
Wed Jan 26 09:23:53 PST 2011


On Jan 26, 2011, at 4:55 AM, Albin Breen wrote:

> Hi! I've been trying out D recently but I'm a little confused about the order in which destructors are called:
> 
> class A {
> 	this() { writeln("ctor"); }
> 	~this() { writeln("dtor"); }
> 
> 	static this() { writeln("static ctor"); }
> 	static ~this() { writeln("static dtor"); }
> }
> 
> void main() {
> 	auto a = new A;
> }
> 
> This will output:
> static ctor
> ctor
> static dtor
> dtor
> 
> I would have thought that the static destructor should be the last one (in case there are static dependencies). Have I missed something?

The static dtors are run when main exits and then the GC terminates, which may trigger a final collection.  In this case, the last "dtor" is when this collection occurs.


More information about the Digitalmars-d mailing list