destructors and GC again

Carlos Santander csantander619 at gmail.com
Mon Oct 16 13:34:22 PDT 2006


Sean Kelly escribió:
> Lutger wrote:
>> Sorry for this topic again, I'm still not exactly sure: when an 
>> unreferenced object is collected during the lifetime of your program 
>> (before main exits), is the destructor of this object guaranteed to be 
>> called? I expect it to be so, as otherwise destructors seem pretty 
>> useless (and dangerous), but cannot infer this from the spec.
> 
> When an object is collected by the GC, it's dtor will be called. 
> However, the GC may not detect an orphaned object as collectable if 
> there is a value in memory somewhere that "looks" like a reference to 
> that object.
> 
> 
> Sean

There's also the problem with global objects:

//-------------------------------------------
import std.stdio;

class A
{
     ~this()
     {
         writefln("~A");
     }
}

A tmp;

void main()
{
     tmp=new A;
}
//-------------------------------------------

Nothing is printed.

-- 
Carlos Santander Bernal



More information about the Digitalmars-d-learn mailing list