Scope statement

Michel Fortin michel.fortin at michelf.com
Tue Feb 5 15:53:55 PST 2008


On 2008-02-05 18:26:50 -0500, Lars V <nospamplease at nospam.com> said:

> But, when the function ends the object should be collected by the GC 
> (there are no more references to it) and the GC will call the 
> destructor, no? I don't understand it.

The garbage collector will generally destruct and deallocate the object 
when you ask for more memory, if there are no more reference to it (it 
doesn't scan memory for pointers each time a function returns). This 
could happen the next time you call new, or in a few more calls to new.

Basically, when to dispose of the object is left to the discretion of 
the garbage collector which may optimize things in various ways you 
shouldn't have to care much about. If you need deterministic 
destruction for your object, either use scope or call the destructor 
yourself (using delete).

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d-learn mailing list