Thread overview
[Issue 5657] New: Temporary object destruction
Feb 27, 2011
SHOO
Apr 01, 2011
SHOO
Apr 13, 2011
Kenji Hara
Apr 14, 2011
Kenji Hara
Apr 14, 2011
Kenji Hara
Apr 17, 2011
Kenji Hara
Apr 20, 2011
Walter Bright
Apr 21, 2011
Walter Bright
Jun 04, 2011
Walter Bright
February 27, 2011
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


--- Comment #0 from SHOO <zan77137@nifty.com> 2011-02-26 23:47:22 PST ---
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: -------
April 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5657



--- Comment #1 from SHOO <zan77137@nifty.com> 2011-04-01 04:44:14 PDT ---
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: -------
April 13, 2011
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


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2011-04-13 14:52:41 PDT ---
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: -------
April 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5657


bearophile_hugs@eml.cc changed:

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


--- Comment #3 from bearophile_hugs@eml.cc 2011-04-13 16:12:05 PDT ---
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: -------
April 14, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5657



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-04-14 01:18:34 PDT ---
Created an attachment (id=942)
Test code.

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



--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2011-04-14 01:21:22 PDT ---
Created an attachment (id=943)
Test results by patched dmd.

(In reply to comment #3)
> 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: -------
April 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5657



--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2011-04-16 19:31:55 PDT ---
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: -------
April 20, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5657


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2011-04-19 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: -------
April 21, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5657



--- Comment #8 from Walter Bright <bugzilla@digitalmars.com> 2011-04-21 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: -------
June 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5657


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #9 from Walter Bright <bugzilla@digitalmars.com> 2011-06-04 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: -------