[Gc] Re: GC needs to be invoked manually

Martin Wartens martin.wartens at mymail.ch
Fri Aug 26 13:56:50 PDT 2005

Meanwhile I discovered that I have to replace "traceable_allocator" 
with "gc_allocator" to make the sample program work. traceable_allocator uses 
MALLOC_UNCOLLECTABLE internally. There seems to be some problem with 
MALLOC_UNCOLLECTABLE. The test program below exposes this problem. There is a 
collectable class Element which creates a dynamic array on initialization. If 
MALLOC_UNCOLLECTABLE is used for allocating the array, no Element object is 
ever collected. This behavior also depends on the size of the array. Collection 
fails if the array has more than 26 entries (=104 bytes).

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

struct Element: public gc_cleanup
	static unsigned created;
	static unsigned destroyed;
	int* dynarray;
	Element(unsigned size)
		/*this does not work*/
		dynarray = (int*) GC_MALLOC_UNCOLLECTABLE( size * sizeof(int) );

		/*the following three variants work*/
		//dynarray = (int*) GC_MALLOC_ATOMIC( size * sizeof(int) );
		//dynarray = (int*) malloc( size * sizeof(int) );
		//dynarray = new int[size];

		for(unsigned i=0; i<size; i++)
			dynarray[i] = 0;

		/*call appropriate free here*/
		//delete[] dynarray;
	Element(const Element& n){ }
unsigned Element::created(0);
unsigned Element::destroyed(0);

//last working size with MALLOC_UNCOLLECTABLE is 26
#define SIZE 27
void testgc(){
	for (unsigned i=0; i<20000; i++)
		Element *root = new Element(SIZE);
	cout << "Elements created: " << Element::created << endl;
	cout << "Elements destroyed: " << Element::destroyed << endl;

int main(int argc,char **argv)

More information about the Gc mailing list