www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1764] New: Scoped sub-objects not garbage collected

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


-- 
Jan 02 2008
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1764


bugzilla digitalmars.com changed:

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




------- Comment #1 from bugzilla digitalmars.com  2008-01-03 00:18 -------
The sub-objects are not guaranteed to be collected when the enclosing object is
deleted, because there is no guarantee there are no other references to those
sub-objects. Because of the way register allocation is done, there may be
references to those sub-objects that persist beyond where logically there are
no more references. This is the way a conservative gc works.

To guarantee they are deleted when the object is deleted, put those deletions
in the object's destructor. You'll have to take responsibility, however, for
there being no other active references to it.


-- 
Jan 02 2008