www.digitalmars.com         C & C++   DMDScript  

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

reply d-bugmail puremagic.com writes:

           Summary: Scoped sub-objects not garbage collected
           Product: D
           Version: 2.008
          Platform: All
               URL: http://www.digitalmars.com/webnews/newsgroups.php?art_gr
        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.

module main;

import std.stdio;

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

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

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

    writefln("2 - Start: Sub class not destroyed");
        scope MyClass a = new MyClass();
    writefln("2 - End:   Sub class not destroyed\n");

    return 0;

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:

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