pthread_key_create() is dangerous

Post Syndicated from Lennart Poettering original https://0pointer.net/blog/projects/pthread-key-create.html

If you use pthread_key_create() with a non-NULL destructor
parameter (or an equivalent TLS construct) in a library/shared object then you
MUST link your library wth -z nodelete (or an equivalent
construct).

If you don’t, then you’ll have a lot of fun (like I just had) debugging
segfaults in the TLS destruction logic where functions are called that might
not even exist anymore in memory.

Now don’t tell me I hadn’t told you.

(Oh, and I hope I don’t need to mention that all GObject-based libraries should
link with -z nodelete anyway, for making sure the type system doesn’t
break.)