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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Aug 15 09:56:45 PDT 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 at puremagic.com
        ReportedBy: cristi.cobzarenco at gmail.com


--- Comment #0 from Cristi Cobzarenco <cristi.cobzarenco at 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: -------


More information about the Digitalmars-d-bugs mailing list