6

If my executable calls dlopen to load a library but neglects to call dlclose, the library will stay loaded until the process exits and the OS forces it to unload.

If I load a.so which loads b.so, then call dlclose on a.so, does the OS unload b.so as well?

How does this compare with a similar scenario using the Microsoft equivalent, LoadLibraryEx?

Kietz
  • 1,186
  • 11
  • 19

1 Answers1

3

The application need only worry about what the app loads directly. If you load a.so, all you need to be concerned with is unloading a.so.

If a.so refuses to unload b.so, that is a problem with a.so, your app is not responsible for this. The author of a.so needs to get their act together and fix the problem with their library.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thanks. Do you know where this information can be found in the documentation for `dl*`? I read http://linux.die.net/man/3/dlopen but didn't find it – Kietz Jan 14 '15 at 20:41
  • I do mostly Windows programming, not Linux/Unix, but I don't think this needs to be officially documented. All a module can do is be responsible for itself wrt loading libraries. What if the library is a third-party one, where the third party may load 1,2,3, or no modules, based on some setting, environment being run on, etc. Is your application going to keep track of this third-party module and what it does internally? Of course not. – PaulMcKenzie Jan 14 '15 at 20:43
  • So this is definitely the case for `LoadLibrary*` and `GetModuleHandle*`? Could you point me at documentation for those? – Kietz Jan 14 '15 at 20:44
  • @Kietz In so much as documentation I don't think that is in there. This is just standard coding practice. The library is responsible for managing itself. You should not be responsible to manage the library unless that is explicitly called out in the documentation. That would be like having to manage the resources of a vector. – NathanOliver Jan 14 '15 at 20:44
  • Okay, thanks all. I was just leery about trusting a C API to clean up resources for me – Kietz Jan 14 '15 at 20:47
  • 1
    @Kietz - If you do discover that the library is not cleaning up resources, contact the author and let them know. – PaulMcKenzie Jan 14 '15 at 20:48
  • Many libraries also have an explicit set of Init / Free functions that you, as a user of the library, are required to call at the appropriate times, usually right after dlopen and right before dlclose. For example, see [SDL_Init](http://sdl.beuc.net/sdl.wiki/SDL_Init) and [SDL_Quit](http://sdl.beuc.net/sdl.wiki/SDL_Quit). So, like PaulMcKenzie says, it is the library developers' responsibility to ensure they don't leak resources like that, but you have to be aware that this is often realized in the form of such Init/Free functions that you, as a user, are responsible for calling. – Mikael Persson Jan 14 '15 at 20:53