www.digitalmars.com         C & C++   DMDScript  

D - Strange crash in destructor

reply Vathix <vathix dprogramming.com> writes:
In this code the destructor crashes the program; I can't even catch the 
exception. Strange thing is that it works fine if name is a char[] or if 
I don't modify its value.


class Foo
{
	char* name;
	
	
	this()
	{
		name = new char[4];
		name[0 .. 4] = "foo\0";
	}
	
	
	~this()
	{
		delete name; //this causes it
	}
}


int main()
{
	Foo foo = new Foo;
	return 0;
}

-- 
Christopher E. Miller
www.dprogramming.com
Feb 04 2004
parent Vathix <vathix dprogramming.com> writes:
That class wasn't needed, this does it:

char* f = new char[4];
f[0] = 'f';
delete f;
Feb 04 2004