Calling destroy on struct pointer

Radu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 25 00:17:50 PST 2017


I'm puzzled by the way destroy works when passed a pointer to a 
struct, observe:
----------------------code.d----------------------
int i;

struct C
{
	this(ref int i)
	{
		++i;
		ii = &i;
	}
	
	~this()
	{
		--(*ii);
	}
	int* ii;
}
unittest
{
     C c = C(i);
     C* cc = &c;
     destroy(cc);
     assert(i == 0); // dtor not called
     destroy(*cc);
     assert(i == 0); // dtor called
}
int main()
{
	
	return 0;
}
----------------------code.d----------------------
destroy(cc) -> does c = C.init
destroy(*cc); -> calls the C dtor

Is this by design? If so - how can I destroy and get the dtor 
called without dereferencing the pointer?
Currently I resort to calling delete on the pointer so dtor gets 
called.



More information about the Digitalmars-d-learn mailing list