dlopen / dlsym / dlclose (dlfcn.h) вызывает утечку памяти - PullRequest
3 голосов
/ 28 июня 2011

При использовании семейства 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?

1 Ответ

3 голосов
/ 28 июня 2011

Похоже на последнее. Однако это не кажется большой проблемой, потому что если вы повторите процедуру dlopen / dlsym / dlclose, вызывая другую подпрограмму, вы увидите, что утечка памяти имеет тот же размер, она не увеличивается с увеличением количества вызовов dlopen / dlclose.

...