Bug 1764 – Scoped sub-objects not garbage collected

Status
RESOLVED
Resolution
WONTFIX
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-01-02T11:59:00Z
Last change time
2015-06-09T01:14:23Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
gide

Comments

Comment #0 by gide — 2008-01-02T11:59:46Z
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
Comment #1 by bugzilla — 2008-01-03T00:18:24Z
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.