[Gc] doubts: gc, gc_cleanup classes

Bruce Hoult bruce at hoult.org
Mon Feb 11 15:22:08 PST 2008


2008/2/12 Renato Tegon Forti <re.tf at terra.com.br>:
> int main(void)
> {
>       GC_INIT();
>       for (size_t i = 0; i < 5; i++) new Test();
>       return 0;
> }
>
> In this ample 'Test' is collectable, Ok?
>
> My destructor doesn't run.

You misunderstand the functioning of GC.  Your destructors should not run.

GC allows you to program as if you had infinite memory available.  It
provides a simulation of infinite memory that works by waiting until
memory runs out and then finding objects that are no longer used by
the program and reusing them.

Your destructors will be run only if your program runs out of memory
and the GC collects your objects.  I would not expect allocating five
small objects to run you out of memory.


If you want some actions to be run at the end of the program that is
not the job of the GC.  You should keep your own list of such actions,
just as you would if you didn't use GC and your computer had infinite
memory.


More information about the Gc mailing list