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


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)];
   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: 
May 19 2013
parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
Yes, because dmd in some cases unexpectedly inserts temporaries.


May 19 2013