[Gc] Determining if an object is valid

Henning Makholm makholm at octoshape.com
Thu Aug 30 03:18:08 PDT 2012

On 2012-08-29 15:35, Hans Aberg wrote:

> Is there an inexpensive way to determine if an object is still valid? Using
> GC_is_heap_ptr(), a C++ object on the function stack can be marked so that
> it moves to the heap when its scope expires. But if all references to it
> are dead, this is unnecessary. There seems no simple way to determine this,
> though.

1. The only way to figure out with certainty if an object is still live is to
    do a full marking phase, which is rather expensive. If you have local scopes
    with such objects opening and closing often, an average one wouldn't even
    live long enough for a GC to take place, in which case EVERY heap object
    ever used during its lifetime would still count as live.

2. You'd need to use some kind of special mechanism to track the objects that
    point to your stack object such that you can rewrite them when you move the
    stack object -- without this tracking mechanism itself keeping the referring
    objects alive at a GC. The GC does support various finalizer tricks that can
    be used for this, but since they need heap allocation, they would almost
    surely be just as expensive as just heap-allocating your "stack" object from
    the beginning.

Henning Makholm
Octoshape ApS

More information about the Gc mailing list