www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - arrays of struct - critical bug?

reply "Jack Applegame" <japplegame gmail.com> writes:
Calling constructors/destructors/postblits for structs in array 
is totally unpredictable.

dmd 2.062 - http://dpaste.1azy.net/445359ad
gdc 2.060 - http://dpaste.1azy.net/7d0bf021
ldc 2.060 - http://dpaste.1azy.net/74034586

Code:

import std.stdio;

struct A {
   this(int a) { m = a; writefln("constructor %s", m); }
   this(this) { writefln("postblit %s", m); }
   ref A opAssign(ref A other) { writefln("assign %s", m); return 
this; }
   ~this() { writefln("destructor %s", m); }
   int m;
}
void main() {
   writeln("-- begin --");
   A[] arr1= [A(1), A(2)];
   writeln("-----------");
   A[2] arr2 = [A(3), A(4)];
   writeln("--- end ---");
}

I believe that each constructor/postblit must correspond to the 
destructor and vice versa.
But we see double/missing destructors and (in case of gdc) even 
destruction of unknown things.
This is breaks in particular RefCounted concept: 
http://dpaste.1azy.net/7241b101
May 19 2013
parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
Yes, because dmd in some cases unexpectedly inserts temporaries.

http://d.puremagic.com/issues/show_bug.cgi?id=9334

http://d.puremagic.com/issues/show_bug.cgi?id=9335
May 19 2013