Hacker News new | ask | show | jobs
by matheusmoreira 1674 days ago
> imagine freeing a pointer allocated with a different malloc implementation, or a different layout of libc structs

Should libraries be allocating memory to begin with? They should simply provide the data structures. The main program should decide whether to allocate on the stack, statically or dynamically.

2 comments

Wait until you learn about (dynamically-loaded) libraries that start up worker threads and stuff without any notification. You do your stuff with them, then call FreeLibrary and suddenly some random thread crashes because its text is no longer mapped and brings your whole program down. Fun stuff!
Jesus. I wonder how much time it took to debug that...

Libaries shouldn't do anything unless called explicitly. Even when called, they should cede as much control as possible. They should probably be exposing the concurrent functions so the caller can decide how to thread them... Every time a library decides to make things easy for the user it leads to insane problems like these.

Nah, it's a rather basic problem and the solution is well known: do another LoadLibrary on the already loaded library. Of course, then you need a Free­Library­AndExit­Thread... [0] And the posts following [0] (you can see them at the bottom of the page in the "Read Next" section) explain more of the context.

All this stuff is there because of the built-in threading support in COM which, arguably, was pretty convoluted for some rather strange reasons ― what's the point of such precise RAII-ing of the DLLs? I guess the address space was really precious back then or something.

[0] https://devblogs.microsoft.com/oldnewthing/20131105-00/?p=27...

Mix in some signals and you are on the fast track to debugging purgatory.
I guess strdup() kind of answers that.