next up previous contents
Next: The Rules Up: Smart Pointers & Garbage-Collected Previous: Runtime Initialization

Object Creation

Giggle can manage the deallocation of objects created on the heap. It only manages those objects you tell it to manage; other objects are managed in the traditional ``manual'' way, with an explicit delete which you type. You make the decision on who destroyes an object when you create the object with a new statement.

To create an object that Giggle manages, use ::operator new (size_t, CyberTiggyr::giggle::GarbageCollector &). To do that, just do this:

#include <CyberTiggyr/giggle/giggle.hxx>

using CyberTiggyr::giggle::Ptr;
using CyberTiggyr::giggle::gc;
using std::string;

...
..
.

Ptr<string> p = new (gc ()) string;

`` new (gc ())'' calls Giggle's special operator new. How & why does it do that? With the value returned by `` gc ()'', which is a reference to the one and only Giggle Garbage Collector object in the system.

Notice that we assign the address of the new string to a Ptr$<$string$>$. Always do this immediately with objects that will be managed by Giggle. As long as some Ptr points to an object, Giggle won't collect it. If no Ptr points to the object, Giggle might collect it.

You can use a Ptr$<>$ like you would any smart pointer. See the header file CyberTiggyr/giggle/Ptr.hxx for the complete interface.

Never delete an object crated with `` new (gc ())''. Doing so is an error; it will cause much wailing & gnashing of teeth. When you create an object with `` new (gc ())'', Giggle monitors the smart pointers that point to that object. When no pointers point to it, or when the only pointers that point to it are themselves unreachable by other objects, Giggle will delete the object.


next up previous contents
Next: The Rules Up: Smart Pointers & Garbage-Collected Previous: Runtime Initialization
Gene Michael Stover
2002-04-28