Skip to content
Permalink
Browse files

- moved all exception handling out of the backends

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 96006eb94f61348c8180eeab237d680cc93386d7
@@ -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)
{
@@ -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
@@ -2348,7 +2353,7 @@ void I_Quit()
//
//==========================================================================

void D_DoomMain (void)
static void D_DoomMain_Internal (void)
{
int p;
const char *v;
@@ -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);
@@ -2739,7 +2746,7 @@ void D_DoomMain (void)

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

V_Init2();
@@ -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
@@ -45,7 +45,7 @@ struct CRestartException
char dummy;
};

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


void D_Display ();
@@ -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);

@@ -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.
@@ -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
@@ -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);
}
@@ -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)
@@ -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)
{
@@ -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_*
@@ -205,6 +199,5 @@ int main (int argc, char **argv)
}

I_StartupJoysticks();
D_DoomMain ();
return 0;
return D_DoomMain ();
}
@@ -103,7 +103,6 @@ void I_Init (void)
// I_Error
//
extern FILE *Logfile;
bool gameisdead;

#ifdef __APPLE__
void Mac_I_FatalError(const char* errortext);
@@ -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
{

0 comments on commit 96006eb

Please sign in to comment.
You can’t perform that action at this time.