Fix segfault when exceptions are thrown on errors.

That was exactly the point of exceptions; to NOT crash the app!
This commit is contained in:
Stephen Anthony 2020-12-20 13:39:28 -03:30
parent b36729a825
commit 60c991e171
4 changed files with 50 additions and 30 deletions

View File

@ -39,9 +39,8 @@ FBBackendSDL2::FBBackendSDL2(OSystem& osystem)
if(SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) if(SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
{ {
ostringstream buf; ostringstream buf;
buf << "ERROR: Couldn't initialize SDL: " << SDL_GetError() << endl; buf << "ERROR: Couldn't initialize SDL: " << SDL_GetError();
Logger::error(buf.str()); throw runtime_error(buf.str());
throw runtime_error("FATAL ERROR");
} }
Logger::debug("FBBackendSDL2::FBBackendSDL2 SDL_Init()"); Logger::debug("FBBackendSDL2::FBBackendSDL2 SDL_Init()");

View File

@ -31,6 +31,7 @@
#include "FBSurface.hxx" #include "FBSurface.hxx"
#include "TIASurface.hxx" #include "TIASurface.hxx"
#include "FrameBuffer.hxx" #include "FrameBuffer.hxx"
#include "PaletteHandler.hxx"
#include "StateManager.hxx" #include "StateManager.hxx"
#include "RewindManager.hxx" #include "RewindManager.hxx"
@ -78,8 +79,7 @@ void FrameBuffer::initialize()
{ {
// First create the platform-specific backend; it is needed before anything // First create the platform-specific backend; it is needed before anything
// else can be used // else can be used
try { myBackend = MediaFactory::createVideoBackend(myOSystem); } myBackend = MediaFactory::createVideoBackend(myOSystem);
catch(const runtime_error& e) { throw e; }
// Get desktop resolution and supported renderers // Get desktop resolution and supported renderers
vector<Common::Size> windowedDisplays; vector<Common::Size> windowedDisplays;
@ -952,7 +952,7 @@ void FrameBuffer::stateChanged(EventHandlerState state)
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string FrameBuffer::getDisplayKey() string FrameBuffer::getDisplayKey() const
{ {
// save current window's display and position // save current window's display and position
switch(myBufferType) switch(myBufferType)
@ -974,7 +974,7 @@ string FrameBuffer::getDisplayKey()
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string FrameBuffer::getPositionKey() string FrameBuffer::getPositionKey() const
{ {
// save current window's display and position // save current window's display and position
switch(myBufferType) switch(myBufferType)
@ -996,13 +996,31 @@ string FrameBuffer::getPositionKey()
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FrameBuffer::saveCurrentWindowPosition() void FrameBuffer::saveCurrentWindowPosition() const
{ {
if(myBackend)
{
myOSystem.settings().setValue( myOSystem.settings().setValue(
getDisplayKey(), myBackend->getCurrentDisplayIndex()); getDisplayKey(), myBackend->getCurrentDisplayIndex());
if(myBackend->isCurrentWindowPositioned()) if(myBackend->isCurrentWindowPositioned())
myOSystem.settings().setValue( myOSystem.settings().setValue(
getPositionKey(), myBackend->getCurrentWindowPos()); getPositionKey(), myBackend->getCurrentWindowPos());
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FrameBuffer::saveConfig(Settings& settings) const
{
// Save the last windowed position and display on system shutdown
saveCurrentWindowPosition();
if(myTIASurface)
{
Logger::debug("Saving TV effects options ...");
tiaSurface().ntsc().saveConfig(settings);
Logger::debug("Saving palette settings...");
tiaSurface().paletteHandler().saveConfig(settings);
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -291,12 +291,12 @@ class FrameBuffer
uInt32 hidpiScaleFactor() const { return myHiDPIEnabled ? 2 : 1; } uInt32 hidpiScaleFactor() const { return myHiDPIEnabled ? 2 : 1; }
/** /**
These methods are used to load/save position and display of the This method should be called to save the current settings of all
current window. its subsystems. Note that the this may be called when the class
hasn't been fully initialized, so we first need to check if the
subsytems actually exist.
*/ */
string getPositionKey(); void saveConfig(Settings& settings) const;
string getDisplayKey();
void saveCurrentWindowPosition();
#ifdef GUI_SUPPORT #ifdef GUI_SUPPORT
/** /**
@ -376,6 +376,14 @@ class FrameBuffer
int scaleY(int y) const { return myBackend->scaleY(y); } int scaleY(int y) const { return myBackend->scaleY(y); }
private: private:
/**
These methods are used to load/save position and display of the
current window.
*/
string getPositionKey() const;
string getDisplayKey() const;
void saveCurrentWindowPosition() const;
/** /**
Calls 'free()' on all surfaces that the framebuffer knows about. Calls 'free()' on all surfaces that the framebuffer knows about.
*/ */

View File

@ -50,7 +50,6 @@
#include "CartCreator.hxx" #include "CartCreator.hxx"
#include "FrameBuffer.hxx" #include "FrameBuffer.hxx"
#include "TIASurface.hxx" #include "TIASurface.hxx"
#include "PaletteHandler.hxx"
#include "TIAConstants.hxx" #include "TIAConstants.hxx"
#include "Settings.hxx" #include "Settings.hxx"
#include "PropsSet.hxx" #include "PropsSet.hxx"
@ -152,8 +151,9 @@ bool OSystem::create()
myFrameBuffer = make_unique<FrameBuffer>(*this); myFrameBuffer = make_unique<FrameBuffer>(*this);
myFrameBuffer->initialize(); myFrameBuffer->initialize();
} }
catch(...) catch(const runtime_error& e)
{ {
Logger::error(e.what());
return false; return false;
} }
@ -258,19 +258,14 @@ void OSystem::loadConfig(const Settings::Options& options)
void OSystem::saveConfig() void OSystem::saveConfig()
{ {
// Ask all subsystems to save their settings // Ask all subsystems to save their settings
if(myFrameBuffer) if(myFrameBuffer && mySettings)
myFrameBuffer->saveConfig(settings());
if(mySettings)
{ {
// Save the last windowed position and display on system shutdown
myFrameBuffer->saveCurrentWindowPosition();
Logger::debug("Saving TV effects options ...");
myFrameBuffer->tiaSurface().ntsc().saveConfig(settings());
Logger::debug("Saving palette settings...");
myFrameBuffer->tiaSurface().paletteHandler().saveConfig(settings());
}
Logger::debug("Saving config options ..."); Logger::debug("Saving config options ...");
mySettings->save(); mySettings->save();
}
if(myPropSet && myPropSet->save(myPropertiesFile)) if(myPropSet && myPropSet->save(myPropertiesFile))
Logger::debug("Saving properties set ..."); Logger::debug("Saving properties set ...");