www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5657] New: Temporary object destruction

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

           Summary: Temporary object destruction
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: critical
          Priority: P5
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: zan77137 nifty.com



Following code doesn't call destructor:
--------------------------
import std.stdio;
struct A
{
    this(int a) {writeln("A.ctor");}
    ~this() {writeln("A.dtor");}
}

void main()
{
    A(1);
}
--------------------------
$ dmd -run main
A.ctor
--------------------------

I suppose that this bug is related to a series of problems about the temporary
object.

The outbreak of construction, destruction, postblit are as following table:
                                 postblit | destructor
constructor  -> parameter    ...    x     |    o
constructor  -> return value ...    x     |    x
constructor  -> variable     ...    x     |    o
constructor  -> no operate   ...    x     |    x
return value -> parameter    ...    x     |    o
return value -> return value ...    x     |    x
return value -> variable     ...    x     |    o
return value -> no operate   ...    x     |    x
variable     -> parameter    ...    o     |    o
variable     -> return value ...    o     |    o
variable     -> variable     ...    o     |    o
variable     -> no operate   ... (error)  | (error)

* When a variable leaves from the scope, destructor is called.
* When a variable returns from the function or a variable copies to other
variable, postblit is called.

Test code is here: http://ideone.com/7ujuN

Are these behaviors correct? I feel suspicious...
I think these behavior should be determined more strictly.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 26 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657




I tested following commit:
https://github.com/D-Programming-Language/dmd/commit/e764b3949ae0f95f8fc4d7d2e9114e29fee12493

with following code:
http://ideone.com/Yqomf

and result is here:
http://ideone.com/qfBnx


CONCLUTION:

Almost!
Its behavior is right except a case to ignore without storing away a temporary
object produced in a return value to a variable.

+--------------+--------------+----------+------------+-------------+
| source       | distination  | postblit | destructor | correctness |
+--------------+--------------+----------+------------+-------------+
| constructor  | parameter    |    x     |     o      |      OK     |
| constructor  | return value |    x     |     x      |      OK     |
| constructor  | variable     |    x     |     o      |      OK     |
| constructor  | no operate   |    x     |     o      |      OK     |
| return value | parameter    |    x     |     o      |      OK     |
| return value | return value |    x     |     x      |      OK     |
| return value | variable     |    x     |     o      |      OK     |
| return value | no operate   |    x     |    _x_     |     _NG_    |
| variable     | parameter    |    o     |     o      |      OK     |
| variable     | return value |    o     |     o      |      OK     |
| variable     | variable     |    o     |     o      |      OK     |
| variable     | no operate   | (error)  |  (error)   |      OK     |
+--------------+--------------+----------+------------+-------------+
                                 (*) x ... not called / o ... called

Minimized case is here:
-----
import std.stdio;

struct A{
    this(int x) {writeln("ctor");}
    ~this(){writeln("dtor");}
}

A foo() { return A(1); }
void main() {
    foo();
}
-----
$ dmd -run main
ctor
-----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 01 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |k.hara.pg gmail.com



I try to fix remains.

Test code:
http://ideone.com/5pxTX

Test result:
http://ideone.com/YeE10

Patch:
https://github.com/9rnsr/dmd/compare/master...tempDtor_fix

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



Code in online paste sites like ideone often gets deleted. So I suggest to
avoid their usage in Bugzilla, and to just inline the text or add attaches.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657




Created an attachment (id=942)
Test code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 14 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657




Created an attachment (id=943)
Test results by patched dmd.


 Code in online paste sites like ideone often gets deleted. So I suggest to
 avoid their usage in Bugzilla, and to just inline the text or add attaches.
Thank you for your advice. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 14 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657




Pull request.

https://github.com/D-Programming-Language/dmd/pull/26

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



19:28:48 PDT ---
The patch fails to call the destructor with the following code:

import std.c.stdio;

struct S
{
    int x = 1;

    int bar() { return x; }

    this(int i)
    {
        printf("ctor %p(%d)\n", &this, i);
        t ~= "a";
    }

    this(this)
    {
        printf("postblit %p\n", &this);
        t ~= "b";
    }

    ~this()
    {
        printf("dtor %p\n", &this);
        t ~= "c";
    }

    static string t;
}

S bar() { return S(1); }

void main()
{
    bar().x += 1;
}

I'll see if I can come up with a solution.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657




16:01:40 PDT ---
https://github.com/D-Programming-Language/dmd/commit/aef37eb0c8986a508ccf185286465b4cbef8a066

This is a rewrite of Kenji's patch, which was in the right direction but needed
to handle a few more cases.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 21 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5657


Walter Bright <bugzilla digitalmars.com> changed:

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



12:39:25 PDT ---
https://github.com/9rnsr/dmd/commit/bf31931a097a30254e2ce14f114ccba97fb3dc9f

https://github.com/D-Programming-Language/dmd/commit/7a73683fa1391c88ab659d453b9104e0ce06293d

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