www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5353] New: clear function is calling the destructor twice

reply d-bugmail puremagic.com writes:
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



---
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: -------
Dec 14 2010
next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5353


nfxjfg gmail.com changed:

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



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: -------
Dec 14 2010
parent "Craig Black" <craigblack2 cox.net> writes:

 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.
Dec 14 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5353




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: -------
Dec 14 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Dec 14 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5353




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: -------
Dec 15 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Dec 15 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Dec 15 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5353


Steven Schveighoffer <schveiguy yahoo.com> changed:

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



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: -------
Dec 15 2010