when is the object destuctor called?

Ben Hinkle bhinkle at mathworks.com
Mon May 22 09:41:47 PDT 2006


"Johan Granberg" <lijat.meREM at OVEgmail.com> wrote in message 
news:e4sj2e$43o$1 at digitaldaemon.com...
>I came across this sentence in the specification
>
> "The garbage collector is not guaranteed to run the destructor for all 
> unreferenced objects."
>
> This made me wonder in what circumstances the destructor WONT bee called.
> Basically I want to do this: An object stores a OpenGL texture (uint) and 
> when the object gets collected/deleted I want to free the texture. I don't 
> care about if the destructor will bee called on program exit, but I need 
> some mechanism for freeing the textures during normal execution to prevent 
> a video memory leak.
>
> So I need to know if I can trust the destructor to do this or it their is 
> some other mechanism I can use.

Personally I would not rely on the using the GC to manage anything other 
than main memory. Why? If the user doubles their main memory but doesn't 
change their graphics memory then the GC might decide to collect object less 
often (since there's much more memory around) and so the graphics memory 
fill up before the GC decides it needs to do a collection. The GC should 
only be used to mange what it knows about: main memory. Anything else is a 
latent bug that depends on the end user's system configuration.
Note that's actually why I don't have any destructors in Cx - they lull 
programmers into a false sense of security. In Cx the GC manages main memory 
and any other resource needs to be managed by the programmer. 





More information about the Digitalmars-d mailing list