c - dlopen/dlsym/dlclose(dlfcn.h)导致内存泄漏

标签 c memory-leaks dlopen

像这样使用dlfcn系列时:

#include <stdio.h>
#include <dlfcn.h>

typedef int(*timefunc_t)(void*);

int main()
{
    timefunc_t fun;
    void* handle;
    handle = dlopen("libc.so.6", RTLD_LAZY);
    fun = (timefunc_t)dlsym(handle, "time");
    printf("time=%d\n", fun(NULL));
    dlclose(handle);
    return 0;
}


它导致内存泄漏:

==28803== Memcheck, a memory error detector
==28803== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==28803== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==28803== Command: ./dl
==28803== 
time=1309249569
==28803== 
==28803== HEAP SUMMARY:
==28803==     in use at exit: 20 bytes in 1 blocks
==28803==   total heap usage: 1 allocs, 0 frees, 20 bytes allocated
==28803== 
==28803== LEAK SUMMARY:
==28803==    definitely lost: 0 bytes in 0 blocks
==28803==    indirectly lost: 0 bytes in 0 blocks
==28803==      possibly lost: 0 bytes in 0 blocks
==28803==    still reachable: 20 bytes in 1 blocks
==28803==         suppressed: 0 bytes in 0 blocks
==28803== Rerun with --leak-check=full to see details of leaked memory
==28803== 
==28803== For counts of detected and suppressed errors, rerun with: -v
==28803== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 6)


我的问题是,这是编程错误,还是dlfcn / libdl.so中的错误?

最佳答案

看起来像后者。但是,这似乎没什么大不了的,因为如果您重复dlopen / dlsym / dlclose调用另一个例程,您会发现内存泄漏的大小相同,并且不会随dlopen / dlclose调用的次数而增加。

关于c - dlopen/dlsym/dlclose(dlfcn.h)导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6503661/

相关文章:

c++ - ATmega8 不支持 JMP 指令

c - 父进程调用fork()后子进程内存泄漏,为什么?

php - 如何使用 xdebug 查找 php 内存泄漏?

c++ - glibc : Test if lib as DF_1_NODELETE flag or if lib has unique symbol

linux - Linux 上 dlopen() 的有效相对路径?

c - 使用 memcpy 将缓冲区中的数据存储到结构中

c++ - 创建和读取数据的速度

C指针和内存泄漏

ios - 非公共(public) NSURLConnection 泄漏

memory-leaks - dlopen 中 valgrind 报告的内存泄漏?