next up previous contents
Next: On the Heap Up: Recipe for Use Previous: Recipe for Use

On the Stack

In this mode, you allocate the garbage collector, tell it when to shutdown, & destroy it. This is most convenient & safest if you allocate it on the stack in main.

So your program looks like this:

    using CyberTiggyr::giggle::DefaultCollectorType;
    using CyberTiggyr::giggle::gc;
    using CyberTiggyr::giggle::setGc;

    int
    main (int argc, char *argv[])
    {
	DefaultCollectorType gc;

        setGc (gc);
        run_my_app ();
        gc.shutdown ();
        return 0;
    }

Some rules include:

  1. You can create your Garbage Collector on the stack or on the heap, but you are responsible for seeing that it is deleted.

  2. Before the Garbage Collector is destroyed, you should tell it to shutdown. Shutdown increases the likelihood that memory will be collected, but even it is not a guarrantee.

  3. After shutdown, the only valid operations on a Garbage Collector are fetching its statistics & destruction.

  4. You can use multiple collectors just by creating a new collector & calling setGc on it, but this is highly discouraged. Only one collector is active at a time, & collectors don't communicate. The main reasons for doing this is to compare performances of different Garbage Collectors in the same program, one at a time.

  5. If you receive a bad_alloc exception while memory allocation is under the control of your Garbage Collector (in other words, after you've given your Garbage Collector to setGc), all bets are off. I recommend telling your Garbage Collector to shutdown & then bailing.

For this to work, Giggle must not send any messages to your Garbage Collector when the program shuts down. So if you forgot to tell your Garbage Collector to shutdown, you'll have leaks. So to exit your program from some place other than falling off main, you'll have to fetch the pointer to your Garbage Collector & tell it to shutdown.


next up previous contents
Next: On the Heap Up: Recipe for Use Previous: Recipe for Use
Gene Michael Stover
2002-04-28