www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6499] New: [GSoC] Destructor not called on object returned by method.

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



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: -------
Aug 15 2011
next sibling parent d-bugmail puremagic.com writes:
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



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



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: -------
Aug 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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                       |



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: -------
Aug 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6499




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: -------
Aug 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6499




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: -------
Aug 16 2011
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Sep 02 2011