Is it safe to call dlclose(NULL)?

dynamic-library, dynamic-loading, linux, posix, shared-libraries

Solution

This is tricky. POSIX states that

if `handle` does not refer to an open object, `dlclose()` returns a non-zero value

from which you could infer that it should detect, for an arbitrary pointer, whether that pointer refers to an open object. The Linux/Glibc version apparently does no such check, so you'll want to check for `NULL` yourself.

[Aside, the Linux manpage isn't very helpful either. It's quite implicit about `libdl` functions' behavior, deferring to POSIX without very clearly claiming conformance.]

Problem

I experience a crash when I pass a `null` pointer to `dlclose`. Should I check for null before calling `dlclose`? POSIX tells nothing about this: http://pubs.opengroup.org/onlinepubs/7908799/xsh/dlclose.html Is it undefined behaviour or a bug in `dlclose` implementation?

Original source

Related problems