Thread overview
[Issue 5353] New: clear function is calling the destructor twice
Dec 14, 2010
Craig Black
Dec 14, 2010
nfxjfg@gmail.com
Dec 14, 2010
Craig Black
Dec 14, 2010
nfxjfg@gmail.com
Dec 15, 2010
Simen Kjaeraas
Dec 15, 2010
Simen Kjaeraas
December 14, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353

           Summary: clear function is calling the destructor twice
           Product: D
           Version: D2
          Platform: All
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: craigblack2@cox.net


--- Comment #0 from Craig Black <craigblack2@cox.net> 2010-12-14 13:24:16 PST ---
struct A
{
  ~this() { writeln("here"); }
}

void main()
{
  A *a = new A;
  clear(*a);
}

prints out:

here
here

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 14, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg@gmail.com


--- Comment #1 from nfxjfg@gmail.com 2010-12-14 14:00:42 PST ---
I think it's only because the GC calls the destructor AGAIN, when the programs exits. This is as designed. First, the clear() function doesn't delete the object. Second, the GC calls dtors on all live objects on exit. Thus, the dtor is called twice.

So, this bug is likely INVALID.

As for the clear() function, its design is an embarrassment.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 14, 2010
> --- Comment #1 from nfxjfg@gmail.com 2010-12-14 14:00:42 PST ---
> I think it's only because the GC calls the destructor AGAIN, when the programs
> exits. This is as designed. First, the clear() function doesn't delete the
> object. Second, the GC calls dtors on all live objects on exit. Thus, the dtor
> is called twice.
>
> So, this bug is likely INVALID.
>
> As for the clear() function, its design is an embarrassment.

I know this is not the case.  Try

struct A
{
  ~this() { writeln("here"); }
}

void main()
{
 A a;
 clear(a);
}

writes "here" 3 times. 

December 14, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353



--- Comment #2 from nfxjfg@gmail.com 2010-12-14 15:16:49 PST ---
Craig reminded me on d.D.bugs that this is a struct, not a class. He's right. Maybe the dtor gets called additionally because temporaries of the struct are copy constructed.

It doesn't have to do with the GC and is a different problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353


Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com


--- Comment #3 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-12-14 16:35:59 PST ---
Highly interesting: copying the clear function of object_.d in druntime to my own module, the destructor is called but once.


void myclear(T)(ref T obj) if (is(T == struct))
{
   static if (is(typeof(obj.__dtor())))
   {
       obj.__dtor();
   }
   auto buf = (cast(void*) &obj)[0 .. T.sizeof];
   auto init = (cast(void*) &T.init)[0 .. T.sizeof];
   buf[] = init[];
}

struct test {
   ~this( ) {
      writeln( "dtor!" );
   }
}

void main( ) {
   test* p = new test;
   clear( *p );
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353



--- Comment #4 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-12-15 02:02:20 PST ---
This looks fixed in the newest beta. Anyone else care to check?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com
         OS/Version|Windows                     |All


--- Comment #5 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-12-15 05:48:18 PST ---
I still see 2 clears on Linux with the latest beta.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|nobody@puremagic.com        |schveiguy@yahoo.com


--- Comment #6 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-12-15 06:19:56 PST ---
Figured out the problem.  The runtime creates a temporary copy of the struct in order to copy over the initial value.

It is fixed by copying the TypeInfo.init data.

I'll check in a change after the beta is released (don't want to interfere with
that).

But long story short, your dtor is getting called twice, but the first time it is called, it's on a default-constructed instance, so it shouldn't cause a problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5353


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #7 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-12-15 12:27:48 PST ---
Fixed changeset:

http://www.dsource.org/projects/druntime/changeset/451

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------