digitalmars.D.bugs - [Issue 9386] New: struct destructor called erroneously
- d-bugmail puremagic.com (75/75) Jan 24 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
- d-bugmail puremagic.com (6/6) Jan 24 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
- d-bugmail puremagic.com (34/34) Jan 24 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
- d-bugmail puremagic.com (65/65) Jan 24 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
- d-bugmail puremagic.com (10/10) Apr 08 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
- d-bugmail puremagic.com (11/11) Apr 09 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
- d-bugmail puremagic.com (10/10) Apr 09 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
- d-bugmail puremagic.com (10/10) May 09 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9386
http://d.puremagic.com/issues/show_bug.cgi?id=9386 Summary: struct destructor called erroneously Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: major Priority: P2 Component: DMD AssignedTo: nobody puremagic.com ReportedBy: pisetta.gianni alice.it 07:26:39 PST --- I had a nasty bug with the std.stdio.File type with the files i had opened and immediately after closed. I striped it down to a bug with the management of dynamic arrays of structs. The code below allocate a dynamic array of Test struct, that output a line for the constructor, postblit constructor and destructor, like the File struct. The output shows that the destructors for each struct are called after constructing the entire array. It isn't the behavior expected, since the struct inside the dynamic array aren't garbage collected yet. I think that the more appropriate behavior is inplace construction without destructor. Here is the code: -------------------------- import std.stdio; struct Test { public string name; public this(string name) { this.name = name; writeln( "Created ", name, "..." ); } public this(this) { writeln( "Copied ", this.name, "..." ); } ~this() { writeln( "Deleted ", this.name ); } } void main(string[] args) { Test[] tests = [ Test( "one" ), Test( "two" ), Test( "three" ), Test( "four" ) ]; foreach( Test test; tests ) { writeln( "Foreach ", test.name ); } } -------------------------- And the output: -------------------------- Created one... Created two... Created three... Created four... Deleted four Deleted three Deleted two Deleted one Copied one... Foreach one Deleted one Copied two... Foreach two Deleted two Copied three... Foreach three Deleted three Copied four... Foreach four Deleted four -------------------------- -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 24 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9386 07:29:07 PST --- I forgot to mention that I'm using dmd 2.061. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 24 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9386 Maxim Fomin <maxim maxim-fomin.ru> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |maxim maxim-fomin.ru --- The problem is that dmd lowers the code to like below: void main() { Test _temp1; Test _temp2; Test _temp3; Test _temp4; Test[] tests = [ Test(), Test(), Test(), Test() ]; //these ctors are not called _temp1.ctor("one"); tests[0] = _temp1; _temp2.ctor("two"); tests[1] = _temp2; _temp3.ctor("three"); tests[2] = _temp3; _temp4.ctor("four"); tests[3] = _temp4; _temp1.dtor(); _temp2.dtor(); _temp3.dtor(); _temp4.dtor(); ... } So, the first 8 lines of output comes from 4 stack temporaries, not array. Perhaps you need to look at issue 9335 and issue 9334 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 24 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9386 13:03:09 PST --- Maybe I didn't explained well the problem in my first message. In issue 9335 the problem is that dtors aren't called for structs in dynamic arrays, which is fine for me because is the gc at collection that call the dtors. In issue 9334 the problem is that postblit and dtors are called at allocation of a dynamic array only if isn't called the default constructor, and i don't know if it is good or bad. But if you see the output, there aren't no lines that say "Copied ...." in the output, so here we have the problem that the dtor is called without the postblit, which invalidates any struct that performs the RAII idiom. If you substitute my Test struct of the example in the first message with the File struct of std.stdio module, you will have a Segmentation Fault in linux or a exception FileNotOpened in windows, and it isn't clear what is going on because the structs aren't collected by the gc, no one have called detach() on the files and nobody suspects that the files in the dynamic array are actually been closed at allocation time. I think the best solution here is to have the struct initialized directly in the heap, without the need to first allocate the struct in the stack, as suggested from Maxim Fomin in issue 9334. Then no need to call postblit and dtors, no overhead. But if it isn't possible, then at least it must call the postblit after the structs are copied in the heap and before the call to the dtors on the stack structs, so that any resource remains retained by the struct. The lowered code must be like this: void main() { Test[] tests = [ Test(), Test(), Test(), Test() ]; //these ctors are not called tests[0].ctor("one"); tests[1].ctor("two"); tests[2].ctor("three"); tests[3].ctor("four"); ... } or like this: void main() { Test _temp1; Test _temp2; Test _temp3; Test _temp4; Test[] tests = [ Test(), Test(), Test(), Test() ]; //these ctors are not called _temp1.ctor("one"); tests[0] = _temp1; tests[0].postblit(); // call to postblit constructor _temp2.ctor("two"); tests[1] = _temp2; tests[1].postblit(); // call to postblit constructor _temp3.ctor("three"); tests[2] = _temp3; tests[2].postblit(); // call to postblit constructor _temp4.ctor("four"); tests[3] = _temp4; tests[3].postblit(); // call to postblit constructor _temp1.dtor(); _temp2.dtor(); _temp3.dtor(); _temp4.dtor(); ... } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 24 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9386 Kenji Hara <k.hara.pg gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |pull, wrong-code https://github.com/D-Programming-Language/dmd/pull/1875 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9386 Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/49be239434d2f1d61d645498773d9525b539427c fix Issue 9386 - struct destructor called erroneously https://github.com/D-Programming-Language/dmd/commit/cfab27e4656a5d5f6fac578976a968f83df27cf0 Issue 9386 - struct destructor called erroneously -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 09 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9386 Walter Bright <bugzilla digitalmars.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED CC| |bugzilla digitalmars.com Resolution| |FIXED -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 09 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9386 Kenji Hara <k.hara.pg gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |dmitry.olsh gmail.com *** Issue 8740 has been marked as a duplicate of this issue. *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 09 2013