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: nob...@puremagic.com ReportedBy: pisetta.gia...@alice.it --- Comment #0 from Gianni Pisetta <pisetta.gia...@alice.it> 2013-01-24 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: -------