X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Futil.cc;fp=src%2Flib%2Futil.cc;h=418d7b3e0f6123810b2f482df524473d5abe2d55;hp=ef203c2bdc82fae52119f0acef4b4d4c2ba27764;hb=8aeb741ccbe2edb528e98a431bf55459a6836a9b;hpb=4ba8772aef261da209bbb882325fd61a8b479fd7 diff --git a/src/lib/util.cc b/src/lib/util.cc index ef203c2bd..418d7b3e0 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef DCPOMATIC_POSIX #include #include @@ -93,7 +94,9 @@ using std::istream; using std::numeric_limits; using std::pair; using std::cout; +using std::bad_alloc; using std::streampos; +using std::set_terminate; using boost::shared_ptr; using boost::thread; using boost::lexical_cast; @@ -272,6 +275,33 @@ LONG WINAPI exception_handler(struct _EXCEPTION_POINTERS *) } #endif +/* From http://stackoverflow.com/questions/2443135/how-do-i-find-where-an-exception-was-thrown-in-c */ +void +terminate () +{ + static bool tried_throw = false; + + try { + // try once to re-throw currently active exception + if (!tried_throw++) { + throw; + } + } + catch (const std::exception &e) { + std::cerr << __FUNCTION__ << " caught unhandled exception. what(): " + << e.what() << std::endl; + } + catch (...) { + std::cerr << __FUNCTION__ << " caught unknown/unhandled exception." + << std::endl; + } + +#ifdef DCPOMATIC_POSIX + stacktrace (cout, 50); +#endif + abort(); +} + /** Call the required functions to set up DCP-o-matic's static arrays, etc. * Must be called from the UI thread, if there is one. */ @@ -308,7 +338,9 @@ dcpomatic_setup () boost::filesystem::path lib = app_contents (); lib /= "lib"; setenv ("LTDL_LIBRARY_PATH", lib.c_str (), 1); -#endif +#endif + + set_terminate (terminate); Pango::init (); libdcp::init (); @@ -926,3 +958,13 @@ time_round_up (DCPTime t, DCPTime nearest) DCPTime const a = t + nearest - 1; return a - (a % nearest); } + +void * +wrapped_av_malloc (size_t s) +{ + void* p = av_malloc (s); + if (!p) { + throw bad_alloc (); + } + return p; +}