Thread overview
[Issue 6499] New: [GSoC] Destructor not called on object returned by method.
Aug 15, 2011
Cristi Cobzarenco
Aug 15, 2011
Kenji Hara
Aug 16, 2011
Walter Bright
Aug 16, 2011
Cristi Cobzarenco
Aug 16, 2011
Kenji Hara
Aug 16, 2011
Cristi Cobzarenco
Sep 02, 2011
David Simcha
August 15, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6499

           Summary: [GSoC] Destructor not called on object returned by
                    method.
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: cristi.cobzarenco@gmail.com


--- Comment #0 from Cristi Cobzarenco <cristi.cobzarenco@gmail.com> 2011-08-15 09:56:42 PDT ---
Program:
import std.stdio;

struct Bar {
    string m = "<not set>";

    this( string s ) { writeln( "Constructor - ", m = s ); }
    this( this )     { writeln( "Postblit    - ", m ); }
    ~this()          { writeln( "Destructor  - ", m ); }
    Bar bar()        { return Bar( "bar" ); }
}

Bar foo() { return Bar( "foo" ); }

void main() {
    foo().bar();
}

Output:
Constructor - foo
Constructor - bar
Destructor  - foo

The object returned by bar() is not destroyed (leading to a memory leak in my
GSoC project). Calling bar() directly, rather than on the result returned by
foo() works properly. Saving the result of bar() in a named variable doesn't
fix  the problem (it creates three objects and destroys only two).

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch, wrong-code
                 CC|                            |k.hara.pg@gmail.com


--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2011-08-15 14:52:17 PDT ---
https://github.com/D-Programming-Language/dmd/pull/313

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


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2011-08-16 01:17:38 PDT ---
https://github.com/D-Programming-Language/dmd/commit/cedaaa8927604ebc8b53ebb53c25e586eccd2755

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


Cristi Cobzarenco <cristi.cobzarenco@gmail.com> changed:

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


--- Comment #3 from Cristi Cobzarenco <cristi.cobzarenco@gmail.com> 2011-08-16 06:18:13 PDT ---
Thanks for the fix Kenji. However, this still doesn't work if bar() is a
template function, i.e:

struct Bar {
    string m = "<not set>";

    this( string s ) { writeln( "Constructor - ", m = s ); }
    this( this )     { writeln( "Postblit    - ", m ); }
    ~this()          { writeln( "Destructor  - ", m ); }

    // NOTE: bar is a template, otherwise it works
    Bar bar()()      { return Bar( "bar" ); }
}

Bar foo() { return Bar( "foo" ); }

void main() {
    foo().bar();
}

Outputs:
Constructor - foo
Constructor - bar
Destructor  - bar

Interestingly, this time it's the one returned by foo() that doesn't get
destroyed, rather than the one returned by bar().

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



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-08-16 11:46:47 PDT ---
https://github.com/D-Programming-Language/dmd/pull/316

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



--- Comment #5 from Cristi Cobzarenco <cristi.cobzarenco@gmail.com> 2011-08-16 12:46:41 PDT ---
Thanks a lot for the fix, this stops the memory leak I had in my project. Hope it gets merged into the head soon.

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


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |dsimcha@yahoo.com
         Resolution|                            |FIXED


--- Comment #6 from David Simcha <dsimcha@yahoo.com> 2011-09-02 09:08:55 PDT ---
The second fix has recently been merged and seems to work.

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