我有一个创建的 GLib 哈希表:
GHashTable *gmem = g_hash_table_new_full(NULL, NULL, (GDestroyNotify) free_memoryaddresses, (GDestroyNotify)free_metadatarecords);
在 Valgrind 中,我收到以下错误:
==19610== 1 errors in context 2 of 2:
==19610== Invalid free() / delete / delete[] / realloc()
==19610== at 0x40291BE: free (vg_replace_malloc.c:427)
==19610== by 0x804939F: free_memoryaddresses (memory.c:361)
==19610== by 0x4077A0F: g_hash_table_remove_all_nodes (ghash.c:533)
==19610== by 0x4078A7F: g_hash_table_remove_all (ghash.c:1345)
==19610== by 0x8048BCA: m61_printstatistics (m61.c:115)
==19610== by 0x80494E9: main (mytest.c:9)
==19610== Address 0x4341b50 is 0 bytes inside a block of size 1 free'd
==19610== at 0x40291BE: free (vg_replace_malloc.c:427)
==19610== by 0x8048994: m61_free (m61.c:51)
==19610== by 0x80494E4: main (mytest.c:8)
我没有内存泄漏。但是如果我注释掉这两个函数中的行,那么我会得到内存泄漏和(错误摘要:来自 1 个上下文的 1 个错误(抑制:0 来自 0))
void free_memoryaddresses(gpointer a)
{ (void)a;
free(a);
}
void free_metadatarecords(gpointer a)
{ (void)a;
free_metadata_record(a);
free(a);
}
这是我的 Valgrind 命令:
G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --leak-check=yes --tool=memcheck --track-origins=yes -v ./mytest
我通过存储从 malloc 接收到的指针来获取内存键。