next up previous contents
Next: Object Creation Up: Smart Pointers & Garbage-Collected Previous: Smart Pointers & Garbage-Collected

Runtime Initialization

Before you do any allocation with Giggle, you must tell Giggle what garbage collector to use. Do this early in your program. Create an instance of your selected garbage collector & hand that object to Giggle. It is appropriate to place the garbage collector on the stack in function main, like this:

#include <CyberTiggyr/giggle/giggle.hxx>

using CyberTiggyr::giggle::ReferenceCounter;
using CyberTiggyr::giggle::setGc;

int
main (...)
{
  ReferenceCounter refcntr;

  setGc (refcntr);
  ...
  ..
  .
}

Class ReferenceCounter implements reference-counted garbage collection for Giggle. You could also you class Boehm or some other class that implements garbage collection for Giggle. You must do this exactly once in each program.9.4

The garbage collector (the ReferenceCounter, in the example above) is your one & only garbage collection object. It must come into existence before any objects that you allocate with Giggle, & it should be destroyed after any objects that you created with Giggle. That's why placing it on the stack in function main is appropriate. You could also make it a global object, so that it is created before main is entered & destroyed after main is exited. Or you could create it dynamically, keep a pointer it to it, & destroy it late in the program.

I've had bad experiences with global objects, so I choose to minimize their use. Creating the garbage collector object dynamically seems like extra effort unless you allow for run-time selection of garbage collectors. So I suggest the on-the-stack-in- main technique.


next up previous contents
Next: Object Creation Up: Smart Pointers & Garbage-Collected Previous: Smart Pointers & Garbage-Collected
Gene Michael Stover
2002-04-28