Never called destructor

Don nospam at nospam.com
Fri Mar 26 13:33:01 PDT 2010


div0 wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> bearophile wrote:
>> div0:
>>>     scope x = new A("x");
>>>     y = new A("y");
>>>     x = y;
>> In my opinion it's better to not reassign references of scoped objects.
>> In real programs where possible it's better to write boring and stupid code :-)
>>
>> Bye,
>> bearophile
> 
> Yeah, I was thinking about that and wondering whether in fact it should
> be an error and disallowed.
> 
> I use scope because I want the instance on the stack for performance,
> and allowing the scope ref to be reassigned buggers things up.
> 
> also consider:
> 
> import std.stdio;
> 
> class A {
> 	string	_instance;
> 
> 	this(string instance) {
> 		_instance = instance;
> 	}
> 
> 	~this() {
> 		writefln("A.~this @ 0x%x: %s", cast(void*)this, _instance);
> 	}
> }
> 
> A test() {
> 	scope	x = new A("x");
> 	auto	y = new A("y");
> 	x = y;
> 	return y;
> }
> 
> void main()
> {
> 	scope z = test();
> 	writefln("main, z @ 0x%x, [%s]", cast(void*)z, z._instance);
> }
> 
> output:
> 
> A.~this @ 0x962E40: y
> main, z @ 0x962E40, [y]
> 
> This is clearly wrong, we are accessing a deleted object, and for some
> reason we aren't getting a double delete of y, which we should.

Same as bug 3285 / bug 3516?


More information about the Digitalmars-d-learn mailing list