[gclist] Boehm gc with STL from gcc 2.95.2?

Benjamin Geer benjamin.geer@btinternet.com
Mon, 17 Apr 2000 20:25:52 +0100


I think there might be a problem with gc_cpp.  The following test
program makes a singly-linked list, in which the links are derived
from gc_cleanup, as is the data in each link.  The output shows that
none of the data objects are reclaimed.  When I change it so that
FooLink is derived from gc instead of gc_cleanup, all of them are
reclaimed.  Perhaps I've misunderstood, but it seems to me that it
should work either way, since there are no circular references
involved.

Ben

#include <gc_cpp.h>
#include <iostream>

using namespace std;

int numCreated = 0;
int numDeleted = 0;

// Each link will contain one of these.
class Foo : public gc_cleanup {
public:
    Foo() {
        ++numCreated;
    }

    ~Foo() {
        ++numDeleted;
    }
};

// Links in a linked list.
struct FooLink : public gc_cleanup {
    Foo* foo;
    FooLink* next;

    FooLink(Foo* arg_foo) : foo(arg_foo), next(0) { }
};

// Makes a linked list with 10000 links, then lets it go out of scope.
void makeFoos() {
    Foo* first = new Foo();
    FooLink* head = new FooLink(first);
    FooLink* tail = head;

    for (int i = 0; i < 9999; ++i) {
        Foo* foo = new Foo;
        FooLink* next = new FooLink(foo);
        tail->next = next;
        tail = next;
    }
}

int main(int argc, char* argv[]) {
    makeFoos();

    GC_gcollect();

    cout << numCreated << " created, " << numDeleted << " deleted" << endl;

    return 0;
}