首页 文章

如何在glib的GMemVTable结构中更改函数指针

提问于
浏览
0

我在Ubuntu 14.04 64位上使用glib . 我正在使用glib的内存监视器(函数g_mem_profile()打印内存使用情况以及尚未释放的内存量) . g_mem_profile显示~3k未被释放 . 分配但未释放的块之一具有252字节大小 .

我想的是:我将GMemVTable中的malloc函数替换为

gpointer test_malloc (gsize nBytes)
{
    if (nBytes == 252)
        printf ("Gotcha!\n");
    return malloc (nBytes);
}

这样我就可以进入调试器,在printf行中设置一个断点,然后检查分配这252个字节的调用堆栈 . 这是我用来替换相应的GMemVTable成员的代码:

static gpointer test_malloc (gsize nBytes)
{
    if (nBytes == 252)
        printf ("Bin hier!!!! %" G_GSIZE_FORMAT, nBytes);
    return malloc (nBytes);
}

static void memory_helper ()
{
    /*kpodcast_mem_vtable.malloc = glib_mem_profiler_table->malloc;*/
    kpodcast_mem_vtable.malloc = test_malloc;
    kpodcast_mem_vtable.realloc = glib_mem_profiler_table->realloc;
    kpodcast_mem_vtable.free = glib_mem_profiler_table->free;
    kpodcast_mem_vtable.calloc = glib_mem_profiler_table->calloc;
    kpodcast_mem_vtable.try_malloc = glib_mem_profiler_table->try_malloc;
    kpodcast_mem_vtable.try_realloc = glib_mem_profiler_table->try_realloc;

}

当我使用此代码时程序崩溃:

*** Error in `./kpodcast': corrupted double-linked list: 0x00000000024de530 ***

当我取消注释第一行以便使用glib_mem_profiler_table-> malloc时它不会崩溃 . 我究竟做错了什么?它必须与我使用函数指针的方式有关 .

非常感谢凯

1 回答

  • 1

    我不知道为什么你会看到这个错误信息,但可能还有另一种方法来完成你想要做的事情 .

    事实证明,GLib具有用于捕获特定大小的内存分配的内置机制,当启用调试功能构建库时启用(因此您可能需要download the GLib source然后构建并安装您自己的库本地副本) . 从GLib发行版中的docs/debugging.txt开始:

    Some code portions contain trap variables that can be set during
    debugging time if G_ENABLE_DEBUG has been defined upon compilation
    (use the --enable-debug=yes option to configure for this, macros.txt
    covers more details).
    Such traps lead to immediate code halts to examine the current
    program state and backtrace.
    Currently, the following trap variables exist:
    
    ...
    
    static volatile gulong g_trap_malloc_size;
            If set to a size > 0, g_free(), g_realloc() and g_malloc()
            respectively, will be intercepted if the size matches the
            size of the corresponding memory block to free/reallocate/allocate.
            This will only work with g_mem_set_vtable (glib_mem_profiler_table)
            upon startup though, because memory profiling is required to match
            on the memory block sizes.
    

    实现的 static volatile 声明和断点触发行为使我认为您的意图是在调试器中加载您的应用程序,然后将值填入此变量并让程序运行 . 当进行指定大小的分配时,库将陷入调试器,允许您检查回溯并查看代码中的分配位置 .

相关问题