我创建了一个 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 接收的指针来获取内存 key 。
最佳答案
事实证明我两次调用了“free”。我忘记了我的“免费”包装器称为 free,而我剩下要做的就是释放元数据,而不是实际的指针。
关于c - GLib 哈希表 : Invalid free(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658598/