Thread overview
destructor ~this() for static "members." doesn't seem to get called
Feb 18, 2007
sclytrack
Feb 18, 2007
Cesar Rabak
Feb 19, 2007
Sclytrack
Feb 19, 2007
Derek Parnell
Re: destructor ~this() for static "members." , (thanks)
Feb 19, 2007
Sclytrack
February 18, 2007
import std.stdio;

class Testb
{
	this()
	{
		writefln("testb constructor");
	}
	~this()
	{
		writefln("testb destructor");		//Doesn't output on screen.
	}
}

class Testy
{
	static Testb inner = null;	//Destructor of Testb doesn't appear to be called.

	this()
	{
		writefln("testy constructor");
		inner = new Testb();
	}
	~this()
	{
		writefln("testy destructor");
	}
}

int main()
{
	writefln("gdc version 0.22");
	Testy t = new Testy();
	return 0;
}


It outputs the following lines:
./a.out
gdc version 0.22
testy constructor
testb constructor
testy destructor


The destructor of testb doesn't get called,
before the termination of the application,
or that is at least my perception.

Go go D, go :-)
February 18, 2007
sclytrack escreveu:
> import std.stdio;
> 
> class Testb
> {
> 	this()
> 	{
> 		writefln("testb constructor");
> 	}
> 	~this()
> 	{
> 		writefln("testb destructor");		//Doesn't output on screen.
> 	}
> }
> 
> class Testy
> {
> 	static Testb inner = null;	//Destructor of Testb doesn't appear to be called.
> 
> 	this()
> 	{
> 		writefln("testy constructor");
> 		inner = new Testb();
> 	}
> 	~this()
> 	{
> 		writefln("testy destructor");
> 	}
> }
> 
> int main()
> {
> 	writefln("gdc version 0.22");
> 	Testy t = new Testy();
> 	return 0;
> }
> 
> 
> It outputs the following lines:
> ./a.out
> gdc version 0.22
> testy constructor
> testb constructor
> testy destructor
> 
> 
> The destructor of testb doesn't get called,
> before the termination of the application,
> or that is at least my perception.
> 
OK, is this supposed to be 'automatic' by the D language definition or a responsibility of the programmer of including an explicit call to testb destructor in the testy one?

February 19, 2007
> > The destructor of testb doesn't get called,
> > before the termination of the application,
> > or that is at least my perception.
> >
> OK, is this supposed to be 'automatic' by the D language definition or a responsibility of the programmer of including an explicit call to testb destructor in the testy one?

It is just that I didn't expect this behavior, anyways, it seems that mcs, C# displays the same behavior. I guess I'll have no choice but to make the call explicit.

Go go D :-)
February 19, 2007
On Sun, 18 Feb 2007 18:44:08 -0300, Cesar Rabak wrote:

> sclytrack escreveu:
>> import std.stdio;
>> 
>> class Testb
>> {
>> 	this()
>> 	{
>> 		writefln("testb constructor");
>> 	}
>> 	~this()
>> 	{
>> 		writefln("testb destructor");		//Doesn't output on screen.
>> 	}
>> }
>> 
>> class Testy
>> {
>> 	static Testb inner = null;	//Destructor of Testb doesn't appear to be called.
>> 
>> 	this()
>> 	{
>> 		writefln("testy constructor");
>> 		inner = new Testb();
>> 	}
>> 	~this()
>> 	{
>> 		writefln("testy destructor");
>> 	}
>> }
>> 
>> int main()
>> {
>> 	writefln("gdc version 0.22");
>> 	Testy t = new Testy();
>> 	return 0;
>> }
>> 
>> 
>> It outputs the following lines:
>> ./a.out
>> gdc version 0.22
>> testy constructor
>> testb constructor
>> testy destructor
>> 
>> 
>> The destructor of testb doesn't get called,
>> before the termination of the application,
>> or that is at least my perception.
>> 
> OK, is this supposed to be 'automatic' by the D language definition or a responsibility of the programmer of including an explicit call to testb destructor in the testy one?

It's the programmer responsibility in this situation. I guess you were expecting that when the main() function ended, that D would go and call the dtor of all the objects still hanging around. Unfortunately, D doesn't do that - you need to explictly do that.

Try this ...

------------
import std.stdio;

class Testb
{
    this()
    {
        writefln("testb constructor");
    }
    ~this()
    {
        writefln("testb destructor");
    }
}

class Testy
{
    static Testb inner = null;

    this()
    {
        writefln("testy constructor");
        inner = new Testb();
    }
    ~this()
    {
        writefln("testy destructor");
        if (inner !is null) delete inner;
    }
    static void dtor()
    {
        if (inner !is null) delete inner;
    }
}

// Module destructor. Called when main ends.
static ~this()
{
    Testy.dtor();
}

int main()
{
    Testy t = new Testy();
    return 0;
}

--------------

-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell
February 19, 2007

> >> The destructor of testb doesn't get called,
> >> before the termination of the application,
> >> or that is at least my perception.
> >>
> > OK, is this supposed to be 'automatic' by the D language definition or a responsibility of the programmer of including an explicit call to testb destructor in the testy one?

== Quote from Derek Parnell (derek@psych.ward)'s article

> It's the programmer responsibility in this situation. I guess you were expecting that when the main() function ended, that D would go and call the dtor of all the objects still hanging around. Unfortunately, D doesn't do that - you need to explictly do that.


Yes, I was kind of expecting that. :-)

Anyways thanks for the examples.


I'm always amazed by the response time in this forum.