GMP terminates when out of memory

Rev. Chris Korda victimofleisure at
Sat Dec 27 23:20:03 CET 2008

In my experience (writing firmware in C++, with many threads and
exceptions), provided resources are completely wrapped--i.e. memory
allocation/deallocation *only* in constructors and destructors--there
shouldn't be problems with leaks. If exceptions occur in a constructor, it's
a pain, because the destructor isn't called, but there are workarounds. The
throwing object has to do some extra bookeeping, but component objects still
take care of themselves.

The more serious problem is that GMP wasn't designed around this approach
from the beginning. Retrofitting exception handling into a huge library like
GMP is an unenviable task, but it could be done by adding try-catch blocks
throughout the C++ wrapper functions and so forth. No doubt there would be a
performance penalty but it's justifiable if error recovery is crucial.

Regarding storing the error code in a thread-safe manner, why not use
thread-local storage? I have seen this technique used successfully, and some
C++ compilers support it directly now.


More information about the gmp-discuss mailing list