Show better description to users when std::bad_alloc happens

This commit is contained in:
sfan5 2023-09-05 19:43:33 +02:00
parent 4ef93fe25f
commit 033128d8dc
4 changed files with 18 additions and 11 deletions

@ -277,9 +277,8 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
#ifdef NDEBUG #ifdef NDEBUG
catch (std::exception &e) { catch (std::exception &e) {
std::string error_message = "Some exception: \""; error_message = "Some exception: ";
error_message += e.what(); error_message.append(debug_describe_exc(e));
error_message += "\"";
errorstream << error_message << std::endl; errorstream << error_message << std::endl;
} }
#endif #endif

@ -32,6 +32,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifdef _MSC_VER #ifdef _MSC_VER
#include <dbghelp.h> #include <dbghelp.h>
#include <windows.h>
#include <eh.h>
#include "version.h" #include "version.h"
#include "filesys.h" #include "filesys.h"
#endif #endif
@ -74,6 +76,13 @@ void fatal_error_fn(const char *msg, const char *file,
abort(); abort();
} }
std::string debug_describe_exc(const std::exception &e)
{
if (dynamic_cast<const std::bad_alloc*>(&e))
return "C++ out of memory";
return std::string("\"").append(e.what()).append("\"");
}
#ifdef _MSC_VER #ifdef _MSC_VER
const char *Win32ExceptionCodeToString(DWORD exception_code) const char *Win32ExceptionCodeToString(DWORD exception_code)

@ -25,11 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gettime.h" #include "gettime.h"
#include "log.h" #include "log.h"
#ifdef _WIN32 #ifdef _MSC_VER
#include <windows.h>
#ifdef _MSC_VER
#include <eh.h>
#endif
#define FUNCTION_NAME __FUNCTION__ #define FUNCTION_NAME __FUNCTION__
#else #else
#define FUNCTION_NAME __PRETTY_FUNCTION__ #define FUNCTION_NAME __PRETTY_FUNCTION__
@ -75,6 +71,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define sanity_check(expr) SANITY_CHECK(expr) #define sanity_check(expr) SANITY_CHECK(expr)
std::string debug_describe_exc(const std::exception &e);
void debug_set_exception_handler(); void debug_set_exception_handler();
@ -86,9 +83,10 @@ void debug_set_exception_handler();
#define BEGIN_DEBUG_EXCEPTION_HANDLER try { #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
#define END_DEBUG_EXCEPTION_HANDLER \ #define END_DEBUG_EXCEPTION_HANDLER \
} catch (std::exception &e) { \ } catch (std::exception &e) { \
std::string e_descr = debug_describe_exc(e); \
errorstream << "An unhandled exception occurred: " \ errorstream << "An unhandled exception occurred: " \
<< e.what() << std::endl; \ << e_descr << std::endl; \
FATAL_ERROR(e.what()); \ FATAL_ERROR(e_descr.c_str()); \
} }
#else #else
// Dummy ones // Dummy ones

@ -42,7 +42,8 @@ int script_exception_wrapper(lua_State *L, lua_CFunction f)
} catch (const char *s) { // Catch and convert exceptions. } catch (const char *s) { // Catch and convert exceptions.
lua_pushstring(L, s); lua_pushstring(L, s);
} catch (std::exception &e) { } catch (std::exception &e) {
lua_pushstring(L, e.what()); std::string e_descr = debug_describe_exc(e);
lua_pushlstring(L, e_descr.c_str(), e_descr.size());
} }
return lua_error(L); // Rethrow as a Lua error. return lua_error(L); // Rethrow as a Lua error.
} }