www.digitalmars.com         C & C++   DMDScript  

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

reply Cristi Cobzarenco <cristi.cobzarenco gmail.com> writes:
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
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