Object.destroy() broken?
bitwise via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 2 23:57:10 PDT 2015
It seems that Object.destory() doesn't handle static arrays
properly. It doesn't destroy the contained elements.
Example:
struct S
{
int x;
this(int x) { writeln("ctor"); }
this(this) { writeln("ctor(postblit)"); }
~this() { writeln("dtor"); }
}
void main(string[] args)
{
S[2]* arr = cast(S[2]*)calloc(1, S.sizeof);
emplace(arr, S(1));
destroy(*arr);
free(arr);
}
output has 5 ctors, and 3 dtors:
ctor
ctor(postblit)
ctor(postblit)
dtor
ctor(postblit)
dtor
ctor(postblit)
dtor
if I modify this overload of destroy() like this, then it's fine:
void destroy(T : U[n], U, size_t n)(ref T obj) if (!is(T ==
struct))
{
foreach(i; 0..n) // +
destroy(obj[i]); // +
obj[] = U.init;
}
output has 5 ctors, and 5 dtors, as expected:
ctor
ctor(postblit)
ctor(postblit)
dtor
dtor
dtor
ctor(postblit)
dtor
ctor(postblit)
dtor
More information about the Digitalmars-d
mailing list