From 2d81852e819bfbd7e530cb1fe005adb272769d4c Mon Sep 17 00:00:00 2001 From: aquanull Date: Wed, 4 Apr 2018 18:28:42 +0800 Subject: [PATCH] Fixes for some file handles and memory leaks. --- src/drivers/win/replay.cpp | 12 +++++++++--- src/emufile.cpp | 1 + src/file.cpp | 7 ++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/drivers/win/replay.cpp b/src/drivers/win/replay.cpp index ca998dcf..fb5cd8d1 100644 --- a/src/drivers/win/replay.cpp +++ b/src/drivers/win/replay.cpp @@ -126,7 +126,10 @@ void UpdateReplayCommentsSubs(const char * fname) { MOVIE_INFO info; FCEUFILE *fp = FCEU_fopen(fname,0,"rb",0); - fp->stream = fp->stream->memwrap(); + EMUFILE *tmp = fp->stream->memwrap(); + if (tmp != fp->stream) + delete fp->stream; + fp->stream = tmp; bool scanok = FCEUI_MovieGetInfo(fp, info, true); delete fp; @@ -151,8 +154,11 @@ void UpdateReplayDialog(HWND hwndDlg) { MOVIE_INFO info; - FCEUFILE* fp = FCEU_fopen(fn,0,"rb",0); - fp->stream = fp->stream->memwrap(); + FCEUFILE *fp = FCEU_fopen(fn,0,"rb",0); + EMUFILE *tmp = fp->stream->memwrap(); + if (tmp != fp->stream) + delete fp->stream; + fp->stream = tmp; bool isarchive = FCEU_isFileInArchive(fn); bool ismovie = FCEUI_MovieGetInfo(fp, info, false); delete fp; diff --git a/src/emufile.cpp b/src/emufile.cpp index 5102f2ce..162e8df0 100644 --- a/src/emufile.cpp +++ b/src/emufile.cpp @@ -86,6 +86,7 @@ void EMUFILE_FILE::truncate(s32 length) #else ftruncate(fileno(fp),length); #endif + // this is probably wrong if mode is "wb" fclose(fp); fp = NULL; open(fname.c_str(),mode); diff --git a/src/file.cpp b/src/file.cpp index a13c04a9..3ef0bf6a 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -284,11 +284,16 @@ FCEUFILE * FCEU_fopen(const char *path, const char *ipsfn, char *mode, char *ext { //if the archive contained no files, try to open it the old fashioned way EMUFILE_FILE* fp = FCEUD_UTF8_fstream(fileToOpen,mode); - if(!fp || (fp->get_fp() == NULL)) + if(!fp) + return 0; + if (fp->get_fp() == NULL) { + //fp is new'ed so it has to be deleted + delete fp; return 0; } + //try to read a zip file { fceufp = TryUnzip(fileToOpen);