GHashTable Leak.



All,

I am trying to figure out what I am not freeing in my simple GHashTable.
I am using valgrind to check for leaks. I know this leak has to do with
the g_hash_table_insert I am doing. But I can't seem to figure out what
to free. Anyone have any advice?

------ Short example program -------

#include <glib.h>

static gboolean
destroy_hash_table(gchar* name, gchar *value, gpointer data)
{
    g_free (name);
    g_free (value);
    return TRUE;
}

int
main ( void )
{
    GHashTable *hash;

    hash = g_hash_table_new (g_int_hash, g_str_equal);

    g_hash_table_insert (hash, g_strdup("cake"), g_strdup("cake
value"));

    g_hash_table_foreach (hash, (GHFunc)destroy_hash_table, NULL);

    g_hash_table_destroy (hash);

    return TRUE;
}

$ valgrind --leak-check=yes --show-reachable=yes ./test
==27908== valgrind-1.0.0, a memory error detector for x86 GNU/Linux.
==27908== Copyright (C) 2000-2002, and GNU GPL'd, by Julian Seward.
==27908== Estimated CPU clock rate is 1404 MHz
==27908== For more details, rerun with: -v
==27908== 
==27908== 
==27908== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==27908== malloc/free: in use at exit: 1076 bytes in 2 blocks.
==27908== malloc/free: 7 allocs, 5 frees, 1188 bytes allocated.
==27908== For counts of detected errors, rerun with: -v
==27908== searching for pointers to 2 not-freed blocks.
==27908== checked 4009536 bytes.
==27908== 
==27908== definitely lost: 0 bytes in 0 blocks.
==27908== possibly lost:   0 bytes in 0 blocks.
==27908== still reachable: 1076 bytes in 2 blocks.
==27908== 
==27908== 1076 bytes in 2 blocks are still reachable in loss record 1 of
1
==27908==    at 0x40042744: malloc (vg_clientfuncs.c:100)
==27908==    by 0x4025D57A: g_malloc (gmem.c:138)
==27908==    by 0x4025E09C: g_mem_chunk_new (gmem.c:669)
==27908==    by 0x4025048B: g_hash_node_new (ghash.c:624)
==27908== 
==27908== LEAK SUMMARY:
==27908==    definitely lost: 0 bytes in 0 blocks.
==27908==    possibly lost:   0 bytes in 0 blocks.
==27908==    still reachable: 1076 bytes in 2 blocks.
==27908== 

Thanks. ss.




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