[Issue 9334] Dtor and postblit for struct heap object are not always called

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 17 08:35:25 PST 2013


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


monarchdodra at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra at gmail.com


--- Comment #1 from monarchdodra at gmail.com 2013-01-17 08:35:18 PST ---
(In reply to comment #0)
> If struct object allocated on heap is default constructed, dtor is not called.
> If one has non-default initializer, dtor (and postblit) is called.
> 
> import std.stdio : writefln;
> 
> struct S
> {
>     int i;
>     this(this) { writefln("%X postbit", i); i = 0;}
>     ~this() { writefln("%X dtor", i); }
> }
> 
> auto foo()
> {
>     S* s = new S(); // add any argument to new to call dtor
> }
> 
> void main()
> {
>     foo();
> }

I don't think so: The postblit (and destructor) you are seeing comes (AFAIK)
from moving a stack allocated S() into the heap, *during* the new.

In both case, the object that is on the heap is never destroyed. D makes no
promises that things get destroyed at the end of a run. (again, AFAIK).

Check this out:
//----
import std.stdio;

struct S
{
   int i;
   this(this) { writefln("%X postbit", i); i = 0;}
   ~this() { writefln("%X dtor", i); }
}

auto foo()
{
   S* s = new S(5); // add any argument to new to call dtor
   writeln("here");
}

void main()
{
   foo();
}
//----
5 postbit
5 dtor
here
//----

As you can see, the dtor we are seeing is *NOT* the one that runs at the end of
the program.

>From a performance point of view, I can question why there is a postblit and a
dtor call at all, but it isn't wrong. You probably don't see it on "default
construction", because the runtime only copies the T.init value (so no postblit
or any of that jazz).

As far as I'm concerned, there is nothing wrong here.

-- 
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