www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Object.destroy() broken?

reply "bitwise" <bitwise.pvt gmail.com> writes:
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
Sep 02 2015
next sibling parent reply "bitwise" <bitwise.pvt gmail.com> writes:
On Thursday, 3 September 2015 at 06:57:12 UTC, bitwise wrote:
 [...]
Nevermind... fixed. https://github.com/D-Programming-Language/druntime/pull/362
Sep 03 2015
parent "bitwise" <bitwise.pvt gmail.com> writes:
On Thursday, 3 September 2015 at 07:25:09 UTC, bitwise wrote:
 On Thursday, 3 September 2015 at 06:57:12 UTC, bitwise wrote:
 [...]
Nevermind... fixed. https://github.com/D-Programming-Language/druntime/pull/362
Derp.. No it's not. https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L2728 Bed time.
Sep 03 2015
prev sibling parent "bitwise" <bitwise.pvt gmail.com> writes:
On Thursday, 3 September 2015 at 06:57:12 UTC, bitwise wrote:
 It seems that Object.destory() doesn't handle static arrays 
 properly. It doesn't destroy the contained elements.

 [...]
https://issues.dlang.org/show_bug.cgi?id=15009 https://github.com/D-Programming-Language/druntime/pull/1375
Sep 03 2015