Hash functions and memory?




Hi All,

	This should be an easy question (I hope).  

	I've been playing with the hash routines in glib and before
	I actually start using them I want to make sure that they
	work as advertised and don't have memory leaks.

	Unfortunately, I'm either using the functions incorrectly
	or incompletely (which is the most likely) or there really
	is a memory leak.  I use ccmalloc to look for lost memory.
	I've learned to trust its output.  I have yet to see it
	report something which wasn't a memory leak.  ccmalloc
	reports on any malloc memory which wasn't freed.  Since I
	will be adding and removing items in my programs I need to
	make sure there isn't any lost memory.

	Attached is one of my test programs.  The program does
	nothing but create a hash table, add 100000 items to this
	table, and then removes/frees them all.

	Compiled using:

	gcc -o hashtest hashtest.c `glib-config --cflags` \
		`glib-config --libs` -lccmalloc -ldl -lm

	Remove "-lccmalloc -ldl -lm" if you don't have ccmalloc.

	BTW: 
% glib-config --version 
1.2.2

	My test machine is a linux box running 2.2.0-pre5.

	Here's why I think there's a leak (output from ccmalloc):		

.---------------.
|ccmalloc report|
=======================================================
| total # of|   allocated | deallocated |     garbage |
+-----------+-------------+-------------+-------------+
|      bytes|     2038472 |      804500 |     1233972 |
+-----------+-------------+-------------+-------------+
|allocations|      101216 |      100010 |        1206 |
+-----------------------------------------------------+
[snip]
* 99.10% = 1.2 MB of garbage allocated in 1205 allocations
|       |
|       |       0x08049044 in <main>
|       |
|       |       0x400184ed in <g_hash_table_insert>
|       |                  at ghash.c:168
|       |
|       |       0x40018c53 in <g_hash_node_new>
|       |                  at ghash.c:369
|       |
|       |       0x4001d54c in <g_mem_chunk_alloc>
|       |                  at gmem.c:663
|       |
|       |       0x4001d036 in <g_malloc>
|       |                  at gmem.c:177
|       |
|       `-----> 0x0804930c in <malloc>
|                          at wrapper.c:325
|
|  0.0% = 52 Bytes of garbage allocated in 1 allocation
|       |
|       |       0x08049044 in <main>
|       |
|       |       0x400184ed in <g_hash_table_insert>
|       |                  at ghash.c:168
|       |
|       |       0x40018c3e in <g_hash_node_new>
|       |                  at ghash.c:365
|       |
|       |       0x4001d1bf in <g_mem_chunk_new>
|       |                  at gmem.c:474
|       |
|       |       0x4001d036 in <g_malloc>
|       |                  at gmem.c:177
|       |
|       `-----> 0x0804930c in <malloc>
|                          at wrapper.c:325


	Please let me know if I am using the hash functions incorrectly or
am missing something.  I would rather use these functions than
write my own hash routines.  Thanks a bunch.

								-Ales

-- Cut here for hashtest.c --
#include <stdio.h>
#include <glib.h>

typedef struct st_blah BLAH;

struct st_blah {
	int number;
};

int 
free_func(gpointer key,
	  gpointer value,
          gpointer user_data)

{
	BLAH *node;

	node = (BLAH *) value;

	/*printf("freeing: %d\n", node->number);*/
	free(node);

	return(TRUE);
}


int
main(int argc, char *argv)
{
	GHashTable *hash_table=NULL;
	BLAH *value;
	int i;
	int keys[100000];

	hash_table = g_hash_table_new(NULL, NULL);

	for (i = 0 ; i < 100000; i++) {
		keys[i] = i;

		value = (BLAH *) malloc(sizeof(BLAH));
		value->number=i;

		g_hash_table_insert(hash_table, &keys[i], value);
	}
	
	printf("Removed: %d\n", g_hash_table_foreach_remove(hash_table, 
		free_func, NULL));

	g_hash_table_destroy(hash_table);
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]