Skip to content

Commit

Permalink
Fix code so that FCEUGI fully constructs properly and is not clobbere…
Browse files Browse the repository at this point in the history
…d after the construction by a memset. Also, some minor code cleanup in related areas.
  • Loading branch information
thor2016 committed Apr 12, 2024
1 parent c97e2c9 commit 94975d7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
41 changes: 22 additions & 19 deletions src/fceu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,27 @@ static unsigned int pauseTimer = 0;


FCEUGI::FCEUGI()
: filename(0),
archiveFilename(0)
{
//printf("%08x",opsize); // WTF?!
}

FCEUGI::~FCEUGI()
{
if (name)
{
free(name);
name = nullptr;
}

if (filename)
{
free(filename);
filename = NULL;
filename = nullptr;
}

if (archiveFilename)
{
free(archiveFilename);
archiveFilename = NULL;
archiveFilename = nullptr;
}
}

Expand Down Expand Up @@ -194,7 +198,7 @@ static void FCEU_CloseGame(void)

if (GameInfo->name) {
free(GameInfo->name);
GameInfo->name = NULL;
GameInfo->name = nullptr;
}

if (GameInfo->type != GIT_NSF) {
Expand Down Expand Up @@ -224,7 +228,7 @@ static void FCEU_CloseGame(void)
FCEU_CloseGenie();

delete GameInfo;
GameInfo = NULL;
GameInfo = nullptr;

currFrameCounter = 0;

Expand All @@ -243,7 +247,7 @@ static void FCEU_CloseGame(void)
uint64 timestampbase;


FCEUGI *GameInfo = NULL;
FCEUGI *GameInfo = nullptr;

void (*GameInterface)(GI h);
void (*GameStateRestore)(int version);
Expand Down Expand Up @@ -308,8 +312,8 @@ void FlushGenieRW(void) {
}
free(AReadG);
free(BWriteG);
AReadG = NULL;
BWriteG = NULL;
AReadG = nullptr;
BWriteG = nullptr;
RWWrap = 0;
}
}
Expand Down Expand Up @@ -375,7 +379,7 @@ static void AllocBuffers() {

static void FreeBuffers() {
FCEU_free(RAM);
RAM = NULL;
RAM = nullptr;
}
//------

Expand All @@ -402,14 +406,14 @@ void ResetGameLoaded(void) {
if (GameInfo) FCEU_CloseGame();
EmulationPaused = 0; //mbg 5/8/08 - loading games while paused was bad news. maybe this fixes it
GameStateRestore = 0;
PPU_hook = NULL;
GameHBIRQHook = NULL;
FFCEUX_PPURead = NULL;
FFCEUX_PPUWrite = NULL;
PPU_hook = nullptr;
GameHBIRQHook = nullptr;
FFCEUX_PPURead = nullptr;
FFCEUX_PPUWrite = nullptr;
if (GameExpSound.Kill)
GameExpSound.Kill();
memset(&GameExpSound, 0, sizeof(GameExpSound));
MapIRQHook = NULL;
MapIRQHook = nullptr;
MMC5Hack = 0;
PEC586Hack = 0;
QTAIHack = 0;
Expand Down Expand Up @@ -474,7 +478,6 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode, bool silen

FCEU_CloseGame();
GameInfo = new FCEUGI();
memset( (void*)GameInfo, 0, sizeof(FCEUGI));

GameInfo->filename = strdup(fp->filename.c_str());
if (fp->archiveFilename != "")
Expand Down Expand Up @@ -1344,7 +1347,7 @@ void UpdateAutosave(void) {
FCEUSS_Save(f, false);
AutoSS = true; //Flag that an auto-savestate was made
free(f);
f = NULL;
f = nullptr;
AutosaveStatus[AutosaveIndex] = 1;
}
}
Expand All @@ -1358,7 +1361,7 @@ void FCEUI_RewindToLastAutosave(void) {
f = strdup(FCEU_MakeFName(FCEUMKF_AUTOSTATE, AutosaveIndex, 0).c_str());
FCEUSS_Load(f);
free(f);
f = NULL;
f = nullptr;

//Set pointer to previous available slot
if (AutosaveStatus[(AutosaveIndex + AutosaveQty - 1) % AutosaveQty] == 1) {
Expand Down
33 changes: 17 additions & 16 deletions src/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,28 @@ struct FCEUGI
FCEUGI();
~FCEUGI();

uint8 *name; //Game name, UTF8 encoding
int mappernum;

EGIT type;
EGIV vidsys; //Current emulated video system;
ESI input[2]; //Desired input for emulated input ports 1 and 2; -1 for unknown desired input.
ESIFC inputfc; //Desired Famicom expansion port device. -1 for unknown desired input.
ESIS cspecial; //Special cart expansion: DIP switches, barcode reader, etc.
EGIPPU vs_ppu; //PPU type for Vs. System
EGIVS vs_type; //Vs. System type
uint8 vs_cswitch; // Switch first and second controllers for Vs. System
uint8 *name = nullptr; //Game name, UTF8 encoding
int mappernum = 0;

EGIT type = GIT_CART;
EGIV vidsys = GIV_USER; //Current emulated video system;
ESI input[2] = { SI_UNSET, SI_UNSET }; //Desired input for emulated input ports 1 and 2; -1 for unknown desired input.
ESIFC inputfc = SIFC_UNSET; //Desired Famicom expansion port device. -1 for unknown desired input.
ESIS cspecial = SIS_NONE; //Special cart expansion: DIP switches, barcode reader, etc.
EGIPPU vs_ppu = GIPPU_USER; //PPU type for Vs. System
EGIVS vs_type = EGIVS_NORMAL; //Vs. System type
uint8 vs_cswitch = SIS_NONE; // Switch first and second controllers for Vs. System

MD5DATA MD5;

//mbg 6/8/08 - ???
int soundrate; //For Ogg Vorbis expansion sound wacky support. 0 for default.
int soundchan; //Number of sound channels.
int soundrate = 0; //For Ogg Vorbis expansion sound wacky support. 0 for default.
int soundchan = 0; //Number of sound channels.

char* filename;
char* archiveFilename;
int archiveCount;
char* filename = nullptr;
char* archiveFilename = nullptr;
int archiveCount = 0;
bool loadedFromTmpFile = false; // Was loaded from temporary file, file most likely no longer exists
};

#endif
6 changes: 3 additions & 3 deletions src/unif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ static int NAME(FCEUFILE *fp) {
namebuf[index] = 0;
FCEU_printf("%s\n", namebuf);

if (!GameInfo->name) {
GameInfo->name = (uint8*)malloc(strlen(namebuf) + 1); //mbg merge 7/17/06 added cast
strcpy((char*)GameInfo->name, namebuf); //mbg merge 7/17/06 added cast
if (GameInfo->name == nullptr)
{
GameInfo->name = (uint8*)strdup(namebuf);
}
return(1);
}
Expand Down

0 comments on commit 94975d7

Please sign in to comment.