我有一个创建的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接收的指针来获取内存键。

最佳答案

原来我曾两次叫“免费”。我忘记了我的“免费”包装器称为免费,而我剩下要做的就是释放元数据,而不是实际的指针。

关于c - GLib哈希表:无效的free(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12658598/

10-11 16:45