www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2341] New: Double destruction without intervening copy

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2341

           Summary: Double destruction without intervening copy
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: andrei metalanguage.com


import std.stdio;

struct A
{
    int id;
    this(int x) { id = x; writeln("Created object from scratch: ", x); }
    this(this) { writeln("Copying object: ", id); }
    ~this() { writeln("Destroying object: ", id); }
}

struct B
{
    A member;
}

B foo()
{
    A a = A(45);
    return B(a);
}

void main()
{
    auto b = foo;
}

The code above prints:
Created object from scratch: 45
Destroying object: 45
Destroying object: 45

Obviously there should be an intervening copy, otherwise the same state gets
destructed twice. The correct output should be:

Created object from scratch: 45
Destroying object: 45
Copying object: 45
Destroying object: 45


-- 
Sep 06 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2341





------- Comment #1 from brunodomedeiros+bugz gmail.com  2008-09-08 09:03 -------
Shouldn't the correct output be:

Created object from scratch: 45
Copying object: 45
Destroying object: 45
Destroying object: 45

? 
After all, the postblit in the "B(a)" expression must run before foo's scope
ends and "a" is destroyed, right?


-- 
Sep 08 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2341





------- Comment #2 from andrei metalanguage.com  2008-09-08 09:45 -------
(In reply to comment #1)
 Shouldn't the correct output be:
 
 Created object from scratch: 45
 Copying object: 45
 Destroying object: 45
 Destroying object: 45
 
 ? 
 After all, the postblit in the "B(a)" expression must run before foo's scope
 ends and "a" is destroyed, right?
 

Correct, thank you. --
Sep 08 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2341


bugzilla digitalmars.com changed:

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




------- Comment #3 from bugzilla digitalmars.com  2008-10-20 22:23 -------
Fixed dmd 2.020


-- 
Oct 20 2008