www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Bug in destructors

reply Rob Grainger <rob_grainger2000 yahoo.co.uk> writes:
The following example is based on one from The D Programming Language. I'd
expect the last assertion in
the unittest to succeed, it fails.

class Buffer {
	private void* data;

	// Constructor
	this () {
		data = malloc(1024);
	}
	// Destructor
	~this() {
		free(data);
		data = null;
	}
}
unittest {
	auto b = new Buffer;
	auto b1 = b;
	clear(b);				// Get rid of b's extra state
	assert (b1.data is null);	// Should be ok, fails
}
Sep 13 2010
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
This was discussed here:
http://www.digitalmars.com/d/archives/digitalmars/D/TDPL_Manual_invocation_=
of_destructor_114875.html

On Mon, Sep 13, 2010 at 8:19 PM, Rob Grainger
<rob_grainger2000 yahoo.co.uk> wrote:
 The following example is based on one from The D Programming Language. I'=
d expect the last assertion in
 the unittest to succeed, it fails.

 class Buffer {
 =A0 =A0 =A0 =A0private void* data;

 =A0 =A0 =A0 =A0// Constructor
 =A0 =A0 =A0 =A0this () {
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0data =3D malloc(1024);
 =A0 =A0 =A0 =A0}
 =A0 =A0 =A0 =A0// Destructor
 =A0 =A0 =A0 =A0~this() {
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0free(data);
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0data =3D null;
 =A0 =A0 =A0 =A0}
 }
 unittest {
 =A0 =A0 =A0 =A0auto b =3D new Buffer;
 =A0 =A0 =A0 =A0auto b1 =3D b;
 =A0 =A0 =A0 =A0clear(b); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 // Get rid of b's extra state
 =A0 =A0 =A0 =A0assert (b1.data is null); =A0 =A0 =A0 // Should be ok, fai=
ls
 }
Sep 13 2010