Disallow destroy(structPtr)?

Nick Treleaven via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 20 10:05:09 PST 2015


Hi,

The following code is supposed to destroy the struct instance:

     import std.stdio;
     struct S{
         ~this(){"destruct".writeln;}
         }
     auto p = new S;
     destroy(p);
     "end".writeln;

It works correctly if I use destroy(*p), but the above code could 
perhaps be statically rejected by object.destroy to help prevent bugs. 
Currently, the pointer p is set to null without calling the destructor 
(with recent dmd the destructor is called, but only after "end" is printed).

Here is the destroy overload:

void destroy(T)(ref T obj)
     if (!is(T == struct) && !is(T == interface) && !is(T == class) && 
!_isStaticArray!T)
{
     obj = T.init;
}


More information about the Digitalmars-d-learn mailing list