【问题标题】:GLib Hash Table: Invalid free()GLib 哈希表:无效的 free()
【发布时间】:2012-09-30 04:20:19
【问题描述】:

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

【问题讨论】:

  • 你从哪里得到哈希表键?
  • @nneonneo,我已经重写了我的问题,因为 valgrind 有旧的代码副本,但总体错误仍然相同。我从 malloc 返回的指针中获取了哈希表键。

标签: c glib


【解决方案1】:

事实证明,我两次调用“免费”。我忘记了我的“免费”包装器叫做 free,我剩下要做的就是释放元数据,而不是实际的指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多