Fix some access violation (NPE).

Due to usage global variables (instances) and the way deleters works, we need to fully delete the old one, before creating the new one.
Otherwise the old's deleter remove instances after the new one has set them.
This commit is contained in:
Andrea Odetti 2020-12-28 15:46:08 +00:00
parent 3d79b7983d
commit 04fe254521

View file

@ -17,7 +17,7 @@
namespace namespace
{ {
std::unique_ptr<Game> game; std::unique_ptr<Game> ourGame;
bool endsWith(const std::string & value, const std::string & ending) bool endsWith(const std::string & value, const std::string & ending)
{ {
@ -155,15 +155,16 @@ void retro_set_video_refresh(retro_video_refresh_t cb)
void retro_run(void) void retro_run(void)
{ {
game->processInputEvents(); ourGame->processInputEvents();
game->executeOneFrame(); ourGame->executeOneFrame();
game->drawVideoBuffer(); ourGame->drawVideoBuffer();
const size_t ms = (1000 + 60 - 1) / 60; // round up const size_t ms = (1000 + 60 - 1) / 60; // round up
RDirectSound::writeAudio(ms); RDirectSound::writeAudio(ms);
} }
bool retro_load_game(const retro_game_info *info) bool retro_load_game(const retro_game_info *info)
{ {
ourGame.reset();
log_cb(RETRO_LOG_INFO, "RA2: %s\n", __FUNCTION__); log_cb(RETRO_LOG_INFO, "RA2: %s\n", __FUNCTION__);
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
@ -175,7 +176,7 @@ bool retro_load_game(const retro_game_info *info)
try try
{ {
game.reset(new Game); std::unique_ptr<Game> game(new Game);
const std::string snapshotEnding = ".aws.yaml"; const std::string snapshotEnding = ".aws.yaml";
const std::string gamePath = info->path; const std::string gamePath = info->path;
@ -197,6 +198,7 @@ bool retro_load_game(const retro_game_info *info)
display_message("Enable Game Focus Mode for better keyboard handling"); display_message("Enable Game Focus Mode for better keyboard handling");
} }
std::swap(ourGame, game);
return ok; return ok;
} }
catch (const std::exception & e) catch (const std::exception & e)
@ -213,7 +215,7 @@ bool retro_load_game(const retro_game_info *info)
void retro_unload_game(void) void retro_unload_game(void)
{ {
game.reset(); ourGame.reset();
log_cb(RETRO_LOG_INFO, "RA2: %s\n", __FUNCTION__); log_cb(RETRO_LOG_INFO, "RA2: %s\n", __FUNCTION__);
} }