[Issue 1764] New: Scoped sub-objects not garbage collected

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 2 09:59:48 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1764

           Summary: Scoped sub-objects not garbage collected
           Product: D
           Version: 2.008
          Platform: All
               URL: http://www.digitalmars.com/webnews/newsgroups.php?art_gr
                    oup=digitalmars.D&article_id=63907
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: gide at nwawudu.com


When scope is used to declare an object, full collect 'std.gc.fullCollect()'
does not destroy sub objects even though there are no references to the sub
objects. Sub objects are only de-allocated when the program exits.

Code
----
module main;

import std.stdio;

class MyClass {
    public:
        this()    {   
            writefln("Constructor %d", ctorCount);
            myNumber = ctorCount; ctorCount++;
            member = new MyClass2(); 
        }
        ~this()   {   
            writefln("Destructor %d", myNumber); 
            //member = null; 
        }
    private:
        static int ctorCount = 0;
        MyClass2 member;
        int myNumber;
};

class MyClass2 {
    public:
        this() {
            writefln("Sub_Constructor2 %d", ctorCount);
            myNumber2 = ctorCount; ctorCount++; 
        }
        ~this() {
            writefln("Sub_Destructor2 %d", myNumber2); 
        }
    private:
        static int ctorCount = 0;
        int myNumber2;
};


int main() {
    writefln("1 - Start: Sub class is destroyed");
    {
        MyClass a = new MyClass(); delete a;
    }
    std.gc.fullCollect();
    writefln("1 - End:   Sub class is destroyed\n");

    writefln("2 - Start: Sub class not destroyed");
    {
        scope MyClass a = new MyClass();
    }
    std.gc.fullCollect();
    writefln("2 - End:   Sub class not destroyed\n");

    return 0;
};

Output
------
1 - Start: Sub class is destroyed
Constructor 0
Sub_Constructor2 0
Destructor 0
Sub_Destructor2 0
1 - End:   Sub class is destroyed

2 - Start: Sub class not destroyed
Constructor 1
Sub_Constructor2 1
Destructor 1
2 - End:   Sub class not destroyed

Sub_Destructor2 1


-- 



More information about the Digitalmars-d-bugs mailing list