GC does not delete subclass

Sean Kelly sean at f4.ca
Tue Dec 18 16:00:17 PST 2007


Gide Nwawudu wrote:
> On Tue, 18 Dec 2007 11:45:49 -0800, Sean Kelly <sean at f4.ca> wrote:
> 
>> Matthias Thurau wrote:
>>> "If the objects are no longer referenced, the GC will destroy those also."
>>>
>>> Thats exact the case: In my example the GC isn t destroying that unreferenced member.
>> Technically, it is.  It's just not destroying it in the collection 
>> immediately following the object's allocation, which isn't guaranteed 
>> anyway.  As I said previously, it's likely that a register still holds 
>> the address of this object when the allocation is triggered, because 
>> basically nothing happens between the object's construction and the 
>> collection.  In a real application, such things are unlikely occur.
>>
>>
> 
>>  ~this()   {   writefln("Destructor %d", myNumber); }
> 
> To help the GC, is Java-like null assignment necessary? The following
> line helps std.gc.fullCollect() to destroy My2Class members.
> 
> ~this()   {   writefln("Destructor %d", myNumber); member = null; }

Not usually.  The problem is this case is a register with the value, not 
a memory location.  I've run into it before when trying to create simple 
unit tests and demos in D.  The easiest way around it that I've found is 
to construct an additional "throw away" object after the object you 
expect to be cleaned up, to overwrite any lingering register data that 
may still point to the old object.  This is handy for tiny test apps, 
but again, I don't think anything needs to be done for real apps.


Sean



More information about the Digitalmars-d mailing list