c语言 - __free_hook没有按预期挂钩

  • 本文关键字:语言 free hook c malloc
  • 更新时间 :
  • 英文 :


从这个堆栈溢出问题的选定答案。我决定也免费记录电话。我做了一个伐木free.但是对free的调用似乎没有正确输出。

#include <malloc.h>
static void *(*old_malloc_hook)(size_t, const void *);
static void *new_malloc_hook(size_t size, const void *caller) {
    void *mem;
    __malloc_hook = old_malloc_hook;
    mem = malloc(size);
    fprintf(stderr, "%p: malloc(%zu) = %pn", caller, size, mem);
    __malloc_hook = new_malloc_hook;
    return mem;
}
static void init_malloc_hooks(void) {
    old_malloc_hook = __malloc_hook;
    __malloc_hook = new_malloc_hook;
}
void (*volatile __malloc_initialize_hook)(void) = init_malloc_hooks;

static void (*old_free_hook)(void*, const void *);
static void new_free_hook(void* p, const void *caller) {
    __free_hook = old_free_hook;
    free(p);
    fprintf(stderr, "%p: free(%p)n", caller, p);
    __free_hook = new_free_hook;
}
static void init_free_hooks(void) {
    old_free_hook = __free_hook;
    __free_hook = new_free_hook;
}
void (*volatile __free_initialize_hook)(void) = init_free_hooks;
_free_initialize_hook

从不调用。您需要删除

static void init_free_hooks(void) {
  old_free_hook = __free_hook;
  __free_hook = new_free_hook;
}
void (*volatile __free_initialize_hook)(void) = init_free_hooks;

并在您的init_malloc_hooks中初始化两个钩子:

static void init_malloc_hooks(void) {
  old_malloc_hook = __malloc_hook;
  __malloc_hook = new_malloc_hook;
  old_free_hook = __free_hook;
  __free_hook = new_free_hook;
}

相关内容

  • 没有找到相关文章

最新更新