[Issue 9386] struct destructor called erroneously

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 24 13:03:11 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9386



--- Comment #3 from Gianni Pisetta <pisetta.gianni at alice.it> 2013-01-24 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: -------


More information about the Digitalmars-d-bugs mailing list