当像这样使用dlfcn系列时:
#include #include 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=%dn", 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/c++开发分享dlopen / dlsym / dlclose(dlfcn.h)导致内存泄漏相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/559874.html