Each CSpace
maintains a list of the roots for the
CSpace garbage collector (GC).
These are seeds for running the mark phase.
In other words, only objects that are reachable from the roots will be
protected from deletion by the next sweep phase.
Typically it is necessary for singletons in applications to be declared as roots.
A gcroot<T*> represents a smart pointer that retains shared ownership
of a heap allocated IObject
of type T
which is registered in a
CSpace
.
This is by far the preferred way for applications to protect objects (and any objects reachable from those objects) from being garbage collected.
The AddGcRoot
and RemoveGcRoot
functions described below are
deprecated and should not be used.
The following methods of a CSpace
are used to add and remove GC roots:
$adt+ CSpace
{
void AddGcRoot(ptr<const IObject> object);
void RemoveGcRoot(ptr<const IObject> object);
};
void AddGcRoot(ptr<const IObject> object)
Add a root for the CSpace garbage collector (GC).
Requires a lock on this CSpace
.
The given object must currently be registered with this CSpace
,
using a call to RegisterGcObject()
or RegisterNonGcObject()
.
The given object must not currently be a root of this CSpace
.
void RemoveGcRoot(ptr<const IObject> object)
Remove a root of the CSpace garbage collector (GC).
Requires a lock on this CSpace
.
The given object must currently be a root of this CSpace
,
using a prior call to AddGcRoot()
.
$function+ void AddGcRoot(ptr<const IObject> object)
{
if (CSpace* cs = GetThreadPtr<CSpace>())
{
AddGcRoot(cs, object);
}
else
{
throw AssertionException("AddGcRoot called without CSpace set in thread local storage");
}
}
$function+ void RemoveGcRoot(ptr<const IObject> object)
{
if (CSpace* cs = GetThreadPtr<CSpace>())
{
RemoveGcRoot(cs, object);
}
else
{
throw AssertionException("RemoveGcRoot called without CSpace set in thread local storage");
}
}