[Gc] Re: GC needs to be invoked manually

Martin Wartens martin.wartens at mymail.ch
Thu Aug 11 13:48:46 PDT 2005


I was able to narrow down the problem to the code given below. Running it gives
Elements created: 20001
Elements destroyed: 0
I still can't see what could be wrong with it.

#include "gc_allocator.h"
#include "gc_cpp.h"
#include <iostream>
using namespace std;

struct Element;
//row of sparse matrix, pointers to elements keyed by unsigned
typedef std::map<unsigned, Element*, std::less<unsigned>, 
traceable_allocator<  std::pair<const unsigned, Element*>  > > Row_T;
//sparse matrix, pointers to rows keyed by unsigned
typedef std::map<unsigned, Row_T, std::less<unsigned>, 
traceable_allocator<  std::pair<const unsigned, Row_T>  > > Matrix_T;

//each element holds a sparse matrix of elements,
//this is a tree-like data structure 
struct Element: public gc_cleanup{
        static unsigned created;
        static unsigned destroyed;
        Matrix_T mat;
        Element(){
                ++created;
        }
        Element(const Element& n){
                ++created;
        }
        ~Element()
        {
                cout<<"delete"<<endl;
                ++destroyed;
        }
};
unsigned Element::created(0);
unsigned Element::destroyed(0);

void testgc(){
        Element* someElement = new Element();
        for (unsigned i=0; i<10000; i++)
        {
                Element *n1 = new Element();
                Element *n2 = new Element();

                n2->mat[1][1] = n1;
                n2->mat[2][2] = n1;
                n2->mat[3][3] = someElement;
                n2->mat[4][4] = someElement;
        }
        cout << "Elements created: " << Element::created << endl;
        cout << "Elements destroyed: " << Element::destroyed << endl;
}

int main()
{
        testgc();
}



More information about the Gc mailing list