how to deallocate memory allocated by dlopen()?

落花浮王杯 提交于 2019-12-11 22:02:20

问题


I am about to fix this issue. I read several queries regarding dlopen but still not clear. It seems dlopen is allocating memory by either calloc or malloc. But how to deallocate this memory?

Similar code pointing leak problem here for "dl"

(snip)
Event alloc_fn: Called allocation function "dlopen"
Event var_assign: Assigned variable "dl" to storage returned from "dlopen(&"libc.so.6",1)"
261             dl = dlopen("libc.so.6", RTLD_LAZY);
At conditional (1): "dl" taking true path
262             if (dl) {
Event noescape: Variable "dl" not freed or pointed-to in function "dlsym"
263                 func = dlsym(dl, "fdopen");
264             }
265             assert(func != NULL);
266         }
Event leaked_storage: Variable "dl" goes out of scope
267         return (*func)(fd, mode);
(snip)

Is it a bug or we need to ignore this? If I need to fix, could any one please guide me to fix it?

Thanks, Boobesh


回答1:


Read carefully dlopen(3) man page. Read also Drepper's paper How To Write Shared Libraries which gives a lot of interesting explanations.

You can release the resources acquired by dlopen by calling dlclose

Be careful about resources used or provided by the plugin that you have dlopen-ed. The plugin might have so called constructor or destructor functions, e.g. functions declared with the GCC function attribute constructor etc....

You could also not care, and not bother to call dlclose hence accepting some leakage. In practice, on Linux, you can call many hundred thousands times dlopen without pain (only your address space will grow), see my manydl.c example.



来源:https://stackoverflow.com/questions/21508145/how-to-deallocate-memory-allocated-by-dlopen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!