Skip to content

Commit

Permalink
- moved all exception handling out of the backends
Browse files Browse the repository at this point in the history
The main catch is now in D_DoomMain, only calling platform specific functions to handle the output for the error.

As a nice side effect, -norun can now be done without an exception, just by exiting D_DoomMain with a special exit code.
  • Loading branch information
coelckers committed Oct 6, 2019
1 parent b5fa08b commit 96006eb
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 289 deletions.
2 changes: 1 addition & 1 deletion src/console/c_console.cpp
Expand Up @@ -917,7 +917,7 @@ int PrintString (int iprintlevel, const char *outline)
return 0; // Don't waste time on calculating this if nothing at all was printed...
}

extern bool gameisdead;
bool gameisdead;

int VPrintf (int printlevel, const char *format, va_list parms)
{
Expand Down
30 changes: 28 additions & 2 deletions src/d_main.cpp
Expand Up @@ -2326,6 +2326,11 @@ void I_FatalError(const char *error, ...)
std::terminate(); // recursive I_FatalErrors must immediately terminate.
}

static void NewFailure ()
{
I_FatalError ("Failed to allocate memory from system heap");
}

//==========================================================================
//
// I_Quit
Expand All @@ -2348,7 +2353,7 @@ void I_Quit()
//
//==========================================================================

void D_DoomMain (void)
static void D_DoomMain_Internal (void)
{
int p;
const char *v;
Expand All @@ -2357,6 +2362,8 @@ void D_DoomMain (void)
FString *args;
int argcount;
FIWadManager *iwad_man;

std::set_new_handler(NewFailure);
const char *batchout = Args->CheckValue("-errorlog");

C_InitConsole(80*8, 25*8, false);
Expand Down Expand Up @@ -2739,7 +2746,7 @@ void D_DoomMain (void)

if (Args->CheckParm("-norun") || batchrun)
{
throw CNoRunExit();
return;
}

V_Init2();
Expand Down Expand Up @@ -2830,6 +2837,25 @@ void D_DoomMain (void)
while (1);
}

int D_DoomMain()
{
int ret = 0;
try
{
D_DoomMain_Internal();
ret = 1337;
}
catch (std::exception &error)
{
I_ShowFatalError(error.what());
ret = -1;
}
// Unless something really bad happened, the game should only exit through this single point in the code.
// No more 'exit', please.
// Todo: Move all engine cleanup here instead of using exit handlers and replace the scattered 'exit' calls with a special exception.
return ret;
}

//==========================================================================
//
// clean up the resources
Expand Down
2 changes: 1 addition & 1 deletion src/d_main.h
Expand Up @@ -45,7 +45,7 @@ struct CRestartException
char dummy;
};

void D_DoomMain (void);
int D_DoomMain (void);


void D_Display ();
Expand Down
19 changes: 3 additions & 16 deletions src/posix/cocoa/i_main.mm
Expand Up @@ -141,11 +141,7 @@ void I_DetectOS()
FArgs* Args; // command line arguments


// Newer versions of GCC than 4.2 have a bug with C++ exceptions in Objective-C++ code.
// To work around we'll implement the try and catch in standard C++.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61759
void OriginalMainExcept(int argc, char** argv);
void OriginalMainTry(int argc, char** argv)
int OriginalMainTry(int argc, char** argv)
{
Args = new FArgs(argc, argv);

Expand All @@ -155,27 +151,20 @@ void OriginalMainTry(int argc, char** argv)
progdir = [[exePath stringByDeletingLastPathComponent] UTF8String];
progdir += "/";

D_DoomMain();
return D_DoomMain();
}

namespace
{

TArray<FString> s_argv;


void NewFailure()
{
I_FatalError("Failed to allocate memory from system heap");
}

int OriginalMain(int argc, char** argv)
{
printf(GAMENAME" %s - %s - Cocoa version\nCompiled on %s\n\n",
GetVersionString(), GetGitTime(), __DATE__);

seteuid(getuid());
std::set_new_handler(NewFailure);

// Set LC_NUMERIC environment variable in case some library decides to
// clear the setlocale call at least this will be correct.
Expand All @@ -190,9 +179,7 @@ int OriginalMain(int argc, char** argv)
vid_defheight = static_cast<int>(screenSize.height);
vid_vsync = true;

OriginalMainExcept(argc, argv);

return 0;
return OriginalMainTry(argc, argv);
}

} // unnamed namespace
Expand Down
35 changes: 2 additions & 33 deletions src/posix/cocoa/i_main_except.cpp
Expand Up @@ -39,41 +39,10 @@
#include "atterm.h"

// Import some functions from i_main.mm
void Mac_I_FatalError(const char* const message);

void OriginalMainTry(int argc, char** argv);

void OriginalMainExcept(int argc, char** argv)
{
try
{
OriginalMainTry(argc, argv);
}
catch(const std::exception& error)
{
const char* const message = error.what();

if (strcmp(message, "NoRunExit"))
{
if (CVMAbortException::stacktrace.IsNotEmpty())
{
Printf("%s", CVMAbortException::stacktrace.GetChars());
}

if (batchrun)
{
Printf("%s\n", message);
}
else
{
Mac_I_FatalError(message);
}
}

exit(-1);
}
catch(...)
{
call_terms();
throw;
}
OriginalMainTry(argc, argv);
}
8 changes: 8 additions & 0 deletions src/posix/cocoa/i_system.mm
Expand Up @@ -147,6 +147,14 @@ void I_PrintStr(const char* const message)
}


void Mac_I_FatalError(const char* const message);

void I_ShowFatalError(const char *message)
{
Mac_I_FatalError(message);
}


int I_PickIWad(WadStuff* const wads, const int numwads, const bool showwin, const int defaultiwad)
{
if (!showwin)
Expand Down
9 changes: 1 addition & 8 deletions src/posix/sdl/i_main.cpp
Expand Up @@ -94,10 +94,6 @@ FArgs *Args;
// CODE --------------------------------------------------------------------


static void NewFailure ()
{
I_FatalError ("Failed to allocate memory from system heap");
}

static int DoomSpecificInfo (char *buffer, char *end)
{
Expand Down Expand Up @@ -170,8 +166,6 @@ int main (int argc, char **argv)
GetVersionString(), GetGitTime(), __DATE__);

seteuid (getuid ());
std::set_new_handler (NewFailure);

// Set LC_NUMERIC environment variable in case some library decides to
// clear the setlocale call at least this will be correct.
// Note that the LANG environment variable is overridden by LC_*
Expand Down Expand Up @@ -205,6 +199,5 @@ int main (int argc, char **argv)
}

I_StartupJoysticks();
D_DoomMain ();
return 0;
return D_DoomMain ();
}
1 change: 0 additions & 1 deletion src/posix/sdl/i_system.cpp
Expand Up @@ -103,7 +103,6 @@ void I_Init (void)
// I_Error
//
extern FILE *Logfile;
bool gameisdead;

#ifdef __APPLE__
void Mac_I_FatalError(const char* errortext);
Expand Down
7 changes: 0 additions & 7 deletions src/utility/doomerrors.h
Expand Up @@ -82,13 +82,6 @@ class CDoomError : public std::exception
char m_Message[MAX_ERRORTEXT];
};

class CNoRunExit : public std::runtime_error
{
public:
CNoRunExit() : std::runtime_error("NoRunExit")
{
}
};

class CRecoverableError : public CDoomError
{
Expand Down

0 comments on commit 96006eb

Please sign in to comment.