[Gc] leak with finalizers

Paolo Molaro lupus at debian.org
Wed Dec 22 09:02:49 PST 2004


Hi.
I distilled a C version from a bug report of a mono user.
The GC seems to leak memory when using finalizers in
some specific way (libgc version 6.3).
The test case follows:

#include <gc/gc.h>

static int fin_counter = 0;
static int alloc_counter = 0;

static void
finalizer (void *o, void *d) {
	fin_counter++;
	/* do nothing */
}

typedef struct {
	void *array;
	void *next;
} Final;

int main (int argc, char* argv[]) {
	Final *list = NULL;
	int count = 0;
	while (1) {
		Final *obj = GC_malloc (sizeof (Final));
		/* random sizes cause the GC to leak */
		obj->array = GC_malloc (5000 + ((int)obj & 0xfff));
		/*obj->array = GC_malloc (5000);*/
		obj->next = list;
		GC_REGISTER_FINALIZER_NO_ORDER (obj, finalizer, NULL, NULL, NULL);
		/* we don't leak if we don't add to a temporary list */
		//list = obj;
		list = obj;
		++count;
		++alloc_counter;
		if (count == 1000) {
			list = NULL;
			count = 0;
			printf ("fin counter: %d, alloc counter: %d\n", fin_counter, alloc_counter);
		}
	}
}

Note that, if a constant size is used in the array GC_malloc(),
there is no leak. There is also no leak if we don't temporary
keep alive the objects in a list.
Setting GC_DUMP_REGULARLY always reports:
0 objects are eligible for immediate finalization
but the finalizer gets executed (at least in this test case).
Thanks.

lupus

-- 
-----------------------------------------------------------------
lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better


More information about the Gc mailing list