Static Constructors

bearophile bearophileHUGS at lycos.com
Sat Oct 4 08:15:04 PDT 2008


Saaa:

> I think I don't get GC use. How do I actually delete one of those fruits 
> then?

The GC deletes an object (usually, but sometimes it doesn't do it because it's not a precise GC) when there are no references to/from it.

So if you keep it just inside an array, and it has no inbound references, and you remove it somehow (for example overwriting it with the last item of the array and then reducing the length of the array by 1), the GC loses the only reference to that objects, and deallocates it.

Note the same thing happens in most languages/systems that have a GC.

If your object manages some other resources beside its memory, for example an open file, you have to close it inside the destructor of your object. If the not precise GC keeps a spurious (wrong) reference to your object (because somewhere an int value looks like a pointer to the object memory block), then you are in troubles...

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list