www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [GSoC] Strange struct destructor-postblit-writeln interaction bug.

reply Cristi Cobzarenco <cristi.cobzarenco gmail.com> writes:
--bcaec520ef999f971804a64e04e4
Content-Type: text/plain; charset=ISO-8859-1

While working on my project, I ran into a very hard to trace bug that I
finally managed to isolate and reproduce on a small scale.

The code:

import std.stdio;
import std.conv;

struct Test {
int x;
 this( int m )     { x = m; }
 this( this )      { writeln( "Postblit: ", x ); }
 ~this()           { x = 42; }
 string toString() { return to!string( x ); }
}

int main(string[] argv) {
 auto a = Test(3);
writeln( a );
return 0;
}

Prints 42 on my computer, instead of 3. The writeln in the postblit
constructor shows that the object is copied 6 times, and only on the last
copy x becomes 42 instead of 3. Somehow the destructor gets called on an
object before it is copied. The copying that causes the problem is in
format.d 1599 as far as I can tell with my debugger.

I don't know if it's high priority for everyone, but it certainly is for me.



---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco

--bcaec520ef999f971804a64e04e4
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

While working on my project, I ran into a very hard to trace bug that I fin=
ally managed to isolate and reproduce on a small scale.<div><br></div><div>=
The code:</div><div><br></div><div><div><div>import std.stdio;</div><div>

import std.conv;</div><div><br></div><div>struct Test {</div><div><span cla=
ss=3D"Apple-tab-span" style=3D"white-space:pre">	</span>int x;</div><div><s=
pan class=3D"Apple-tab-span" style=3D"white-space:pre">	</span></div><div><=
span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>this( int m=
 ) =A0 =A0 { x =3D m; }</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>this(=
 this ) =A0 =A0 =A0{ writeln( &quot;Postblit: &quot;, x ); }</div><div><spa=
n class=3D"Apple-tab-span" style=3D"white-space:pre">	</span></div><div><sp=
an class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>~this() =A0 =
=A0 =A0 =A0 =A0 { x =3D 42; }</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span></div=
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>stri=

iv> int main(string[] argv) {</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>auto = a =3D Test(3);</div><div><span class=3D"Apple-tab-span" style=3D"white-spac= e:pre"> </span>writeln( a );</div><div><span class=3D"Apple-tab-span" style= =3D"white-space:pre"> </span>return 0;</div> <div>}</div></div></div><div><br></div><div>Prints 42 on my computer, inste= ad of 3. The writeln in the postblit constructor shows that the object is c= opied 6 times, and only on the last copy x becomes 42 instead of 3. Somehow= the destructor gets called on an object before it is copied. The copying t= hat causes the problem is in format.d 1599 as far as I can tell with my deb= ugger.</div> <div><br></div><div>I don&#39;t know if it&#39;s high priority for everyone= , but it certainly is for me.</div><div><br></div><div><br clear=3D"all"><d= iv><br></div><div>---</div>Cristi Cobzarenco<div>BSc in Artificial Intellig= ence and Computer Science</div> <div>University of Edinburgh<br>Profile: <a href=3D"http://www.google.com/p= rofiles/cristi.cobzarenco" target=3D"_blank">http://www.google.com/profiles= /cristi.cobzarenco</a></div><br> </div> --bcaec520ef999f971804a64e04e4--
Jun 22 2011
parent reply dsimcha <dsimcha yahoo.com> writes:
== Quote from Cristi Cobzarenco (cristi.cobzarenco gmail.com)'s article
 --bcaec520ef999f971804a64e04e4
 Content-Type: text/plain; charset=ISO-8859-1
 While working on my project, I ran into a very hard to trace bug that I
 finally managed to isolate and reproduce on a small scale.
 The code:
 import std.stdio;
 import std.conv;
 struct Test {
 int x;
  this( int m )     { x = m; }
  this( this )      { writeln( "Postblit: ", x ); }
  ~this()           { x = 42; }
  string toString() { return to!string( x ); }
 }
 int main(string[] argv) {
  auto a = Test(3);
 writeln( a );
 return 0;
 }
 Prints 42 on my computer, instead of 3. The writeln in the postblit
 constructor shows that the object is copied 6 times, and only on the last
 copy x becomes 42 instead of 3. Somehow the destructor gets called on an
 object before it is copied. The copying that causes the problem is in
 format.d 1599 as far as I can tell with my debugger.
 I don't know if it's high priority for everyone, but it certainly is for me.

This is definitely a pretty severe bug and I can reproduce it. Please file a bug report in Bugzilla. (http://d.puremagic.com/issues/) It's ok to occasionally bring up very high priority bugs on this newsgroup, but they should **always** be filed in Bugzilla as well because otherwise they get forgotten.
Jun 22 2011
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 6/22/11 10:13 AM, dsimcha wrote:
 == Quote from Cristi Cobzarenco (cristi.cobzarenco gmail.com)'s article
 --bcaec520ef999f971804a64e04e4
 Content-Type: text/plain; charset=ISO-8859-1
 While working on my project, I ran into a very hard to trace bug that I
 finally managed to isolate and reproduce on a small scale.
 The code:
 import std.stdio;
 import std.conv;
 struct Test {
 int x;
   this( int m )     { x = m; }
   this( this )      { writeln( "Postblit: ", x ); }
   ~this()           { x = 42; }
   string toString() { return to!string( x ); }
 }
 int main(string[] argv) {
   auto a = Test(3);
 writeln( a );
 return 0;
 }
 Prints 42 on my computer, instead of 3. The writeln in the postblit
 constructor shows that the object is copied 6 times, and only on the last
 copy x becomes 42 instead of 3. Somehow the destructor gets called on an
 object before it is copied. The copying that causes the problem is in
 format.d 1599 as far as I can tell with my debugger.
 I don't know if it's high priority for everyone, but it certainly is for me.

This is definitely a pretty severe bug and I can reproduce it. Please file a bug report in Bugzilla. (http://d.puremagic.com/issues/) It's ok to occasionally bring up very high priority bugs on this newsgroup, but they should **always** be filed in Bugzilla as well because otherwise they get forgotten.

Please put [GSoC] in the title. Andrei
Jun 22 2011