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

Cristi Cobzarenco cristi.cobzarenco at gmail.com
Wed Jun 22 07:45:02 PDT 2011


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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110622/667f18ad/attachment.html>


More information about the Digitalmars-d mailing list