3

How does one handle fatal errors in shared libraries?

I am wondering because the GNU Multiprecision Library (GMP) provides no means to recover from errors like out-of-memory. Using longjmp() or throwing C++ exceptions would work, except that it leaves GMP in an inconsistent state as per https://gmplib.org/projects.html. Is there a way to unload GMP (or another shared library) and then reload it, to fix this problem?

Demi
  • 3,535
  • 5
  • 29
  • 45
  • For unloading a shared-library: http://stackoverflow.com/questions/8792363/c-dlclose-doesnt-unload-the-shared-library – Brandon Jan 11 '14 at 05:35
  • 1
    Many systems (including linux with the default configuration) can't recover from out-of-memory. Several software get away with the "throwing exceptions" solution, even if it may leak a bit of memory each time (unless you are willing to release all gmp memory at that point). The cleanest solution I can think of is to do the gmp computations in a different process, so that you can kill it. Unloading the library doesn't seem to serve any purpose. – Marc Glisse Jan 11 '14 at 08:55
  • The reason is that GMP would be in an internally inconsistent state, so further use of GMP would cause undefined behavior. The idea is to essentially reset the state of GMP to prevent this. – Demi Jan 12 '14 at 02:22
  • 1
    There is almost no internal state in GMP (3 pointers to allocation functions, not much more). What can become inconsistent is variables, and that's not something that unloading a library can help with. Further use of GMP is not undefined, only further use of the output variables involved in the OOM operation is. – Marc Glisse Jan 12 '14 at 14:32

0 Answers0