From 0a9ba07e540c0344aae8a18b08ce8d27ac4dcc05 Mon Sep 17 00:00:00 2001 From: negativeExponent Date: Fri, 25 Sep 2020 08:08:14 +0800 Subject: [PATCH] Prevent potential memory leaks --- mednafen/FileStream.cpp | 1 + mednafen/cdrom/CDAccess_CHD.cpp | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/mednafen/FileStream.cpp b/mednafen/FileStream.cpp index b0354ce1..33f3a5de 100644 --- a/mednafen/FileStream.cpp +++ b/mednafen/FileStream.cpp @@ -51,6 +51,7 @@ FileStream::FileStream(const char *path, const int mode) FileStream::~FileStream() { + close(); } uint64 FileStream::read(void *data, uint64 count, bool error_on_eos) diff --git a/mednafen/cdrom/CDAccess_CHD.cpp b/mednafen/cdrom/CDAccess_CHD.cpp index 95db52df..364a3971 100644 --- a/mednafen/cdrom/CDAccess_CHD.cpp +++ b/mednafen/cdrom/CDAccess_CHD.cpp @@ -214,10 +214,21 @@ bool CDAccess_CHD::Load(const std::string &path, bool image_memcache) return true; } -CDAccess_CHD::~CDAccess_CHD() +void CDAccess_CHD::Cleanup(void) { - if (chd != NULL) - chd_close(chd); + if (chd != NULL) + chd_close(chd); + + if (hunkmem != NULL) + { + free(hunkmem); + hunkmem = NULL; + } +} + +CDAccess_CHD::~CDAccess_CHD() +{ + Cleanup(); } bool CDAccess_CHD::Read_CHD_Hunk_RAW(uint8_t *buf, int32_t lba, CHDFILE_TRACK_INFO* track)