[Gc] Multiple inheritance issue

Christian Gudrian Christian.Gudrian at gmx.de
Fri Oct 13 13:32:16 PDT 2006


Hello!

I am currently having a problem where the garbage collector prematurely
reclaims an object which is an instance of a class with two base classes:

// --------------------------------------------------
#include "gc.h"
#include "gc_cpp.h"

class IFoo
{
public:
    virtual void foo() = 0;
};

class Foo : public gc_cleanup, public IFoo
{
public:
    ~Foo() {}
    void foo() {}
};

Foo * f = new Foo();
IFoo * i = f;

int main()
{
    f = 0;
    GC_gcollect();
    i->foo();
    return 0;
}
// --------------------------------------------------

The second call to GC_gcollect() destroys the Foo instance, though i
still holds a live pointer to it.  The foo() method then acts upon an
invalid instance.

At the beginning of main() both GC_base(f) and GC_base(i) point to the
same location in memory.  Furthermore GC_is_static_root(&i) and
GC_is_static_root(&f) both return 1.  What makes the garbage collector
ignore the reference to the instance in i?  And how do I work around
this behaviour?

Christian

P.S.: I'm using the garbage collector on Windows XP with Borland
C++Builder 2006.


More information about the Gc mailing list