From 2d6477ce1bd896e9929a24d937fc6a77a8ab06b6 Mon Sep 17 00:00:00 2001 From: shashclp Date: Sun, 19 Jul 2009 01:23:43 +0000 Subject: [PATCH] - Fixed emulator crash on sample loading failure --- desmume/src/mic.h | 2 +- desmume/src/windows/main.cpp | 9 ++++++++- desmume/src/windows/mic.cpp | 33 +++++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/desmume/src/mic.h b/desmume/src/mic.h index 801d6d118..4bbdea231 100644 --- a/desmume/src/mic.h +++ b/desmume/src/mic.h @@ -25,7 +25,7 @@ extern int MicButtonPressed; #ifdef WIN32 static char MicSampleName[256]; -char* LoadSample(const char *name); +bool LoadSample(const char *name); #endif extern int MicDisplay; diff --git a/desmume/src/windows/main.cpp b/desmume/src/windows/main.cpp index 8906dd3b2..82e9d2ae4 100644 --- a/desmume/src/windows/main.cpp +++ b/desmume/src/windows/main.cpp @@ -3798,7 +3798,14 @@ LRESULT CALLBACK MicrophoneSettingsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, WritePrivateProfileInt("Use Mic Sample", "UseMicSample", ((UseMicSample == true) ? 1 : 0), IniName); WritePrivateProfileString("Use Mic Sample", "MicSampleFile", MicSampleName, IniName); - LoadSample(MicSampleName); + + if (UseMicSample) + { + if (!LoadSample(MicSampleName)) + { + MessageBox(hDlg, "Unable to read the sample", "DeSmuME", (MB_OK | MB_ICONEXCLAMATION)); + } + } } } case IDCANCEL: diff --git a/desmume/src/windows/mic.cpp b/desmume/src/windows/mic.cpp index a282831c9..319583293 100644 --- a/desmume/src/windows/mic.cpp +++ b/desmume/src/windows/mic.cpp @@ -57,23 +57,36 @@ static int CALLBACK waveInProc(HWAVEIN wavein, UINT msg, DWORD instance, DWORD p return 0; } -static char* samplebuffer; -static int samplebuffersize; -static FILE* fp; +static char* samplebuffer = NULL; +static int samplebuffersize = 0; +static FILE* fp = NULL; -char* LoadSample(const char *name) +bool LoadSample(const char *name) { - std::ifstream fl(name); + + if (!fl.is_open()) + { + return false; + } + fl.seekg( 0, std::ios::end ); size_t len = fl.tellg(); - samplebuffersize=len; - char *ret = new char[len]; + + // Avoid mem leaks + if (samplebuffer != NULL) + delete[] samplebuffer; + + samplebuffersize = len; + samplebuffer = new char[len]; + fl.seekg(0, std::ios::beg); - fl.read(ret, len); - samplebuffer=ret; + fl.read (samplebuffer, len); + fl.close(); + SampleLoaded=1; - return ret; + + return true; } BOOL Mic_Init() {