Syedsma
Syedsma

Reputation: 1243

dlopen and dlclose memory management in C appln

I use dlopen to load a dynamic library say "lib1.so" and call one exposed function say A1, A1 function allocate a dynamic memory of 100kb using malloc but not deallocate that, in the main function again I all dlclose. [dlopen, call function A1 , dlclose]

I repeate the step say 10 times, Purify report this as memory leak of 1000KB , valgrind reports Indirectly lost 1000KB.

Could you please suggest 100 KB * 10 times = 1000KB , Is a real memory leak? As I have called dlclose, so all memory allocated for dynamic libs are automaticaly freed when we call dlclose?

OS: Linux Programming lan : C

Upvotes: 1

Views: 3960

Answers (3)

onemasse
onemasse

Reputation: 6584

The man page for dlclose doesn't say anything about freeing memory when it's called.

dlclose()
   The  function  dlclose()  decrements the reference count on the dynamic
   library handle handle.  If the reference count drops  to  zero  and  no
   other  loaded  libraries use symbols in it, then the dynamic library is
   unloaded.

   The function dlclose() returns 0 on success, and nonzero on error.

No magic.

If you're using linux you could try this method to wrap malloc() and keep track of the allocated memory.

Upvotes: 3

cyco130
cyco130

Reputation: 4934

dlclose doesn't free memory allocated with malloc. It only frees the static variables declared in the library. You should explicitly free any allocated memory in your library, possibly in the _fini function.

Upvotes: 5

trojanfoe
trojanfoe

Reputation: 122381

No, memory is reclaimed when the process terminates, not when dynamic libraries are closed.

Upvotes: 2

Related Questions