From 0a8a495aea47ef54a3f79d211ce97cd34243d280 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Thu, 25 Feb 2010 21:45:39 +0000 Subject: [PATCH] Fix a smattering of memory leaks. Originally committed to SVN as r4157. --- aegisub/src/audio_provider_hd.cpp | 6 ++---- aegisub/src/audio_provider_ram.cpp | 4 ++-- aegisub/src/auto4_lua.cpp | 18 ++++++++++++------ aegisub/src/dialog_progress.cpp | 6 +++--- aegisub/src/dialog_style_manager.cpp | 8 ++++++-- aegisub/src/mkv_wrap.cpp | 1 - aegisub/src/mkv_wrap.h | 1 + 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/aegisub/src/audio_provider_hd.cpp b/aegisub/src/audio_provider_hd.cpp index edb982fca..8704322a8 100644 --- a/aegisub/src/audio_provider_hd.cpp +++ b/aegisub/src/audio_provider_hd.cpp @@ -98,10 +98,8 @@ HDAudioProvider::HDAudioProvider(AudioProvider *source) { file_cache.Seek(0); // Finish - if (!canceled) { - progress->Destroy(); - } - else { + progress->Destroy(); + if (canceled) { file_cache.Close(); delete[] data; throw wxString(_T("Audio loading cancelled by user")); diff --git a/aegisub/src/audio_provider_ram.cpp b/aegisub/src/audio_provider_ram.cpp index 3bdce5b36..7cba228c7 100644 --- a/aegisub/src/audio_provider_ram.cpp +++ b/aegisub/src/audio_provider_ram.cpp @@ -104,8 +104,8 @@ RAMAudioProvider::RAMAudioProvider(AudioProvider *source) { } // Clean up progress - if (!canceled) progress->Destroy(); - else { + progress->Destroy(); + if (canceled) { Clear(); throw wxString(_T("Audio loading cancelled by user")); } diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index 0200b27a9..34a31ab56 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -254,9 +254,9 @@ namespace Automation4 { // load user script LuaScriptReader script_reader(GetFilename()); if (lua_load(L, script_reader.reader_func, &script_reader, GetPrettyFilename().mb_str(wxConvUTF8))) { - wxString *err = new wxString(lua_tostring(L, -1), wxConvUTF8); - err->Prepend(_T("Error loading Lua script \"") + GetPrettyFilename() + _T("\":\n\n")); - throw err->wx_str(); + wxString err(lua_tostring(L, -1), wxConvUTF8); + err.Prepend(_T("Error loading Lua script \"") + GetPrettyFilename() + _T("\":\n\n")); + throw err; } _stackcheck.check_stack(1); // and execute it @@ -264,9 +264,9 @@ namespace Automation4 { // don't thread this, as there's no point in it and it seems to break on wx 2.8.3, for some reason if (lua_pcall(L, 0, 0, 0)) { // error occurred, assumed to be on top of Lua stack - wxString *err = new wxString(lua_tostring(L, -1), wxConvUTF8); - err->Prepend(_T("Error initialising Lua script \"") + GetPrettyFilename() + _T("\":\n\n")); - throw err->wx_str(); + wxString err(lua_tostring(L, -1), wxConvUTF8); + err.Prepend(_T("Error initialising Lua script \"") + GetPrettyFilename() + _T("\":\n\n")); + throw err; } _stackcheck.check_stack(0); lua_getglobal(L, "version"); @@ -313,6 +313,12 @@ namespace Automation4 { name = GetPrettyFilename(); description = e; } + catch (const wxString& e) { + Destroy(); + loaded = false; + name = GetPrettyFilename(); + description = e; + } catch (Script *s) { // Be sure to properly propagate any scripts throw throw s; diff --git a/aegisub/src/dialog_progress.cpp b/aegisub/src/dialog_progress.cpp index 359778e0b..61c6c1dfe 100644 --- a/aegisub/src/dialog_progress.cpp +++ b/aegisub/src/dialog_progress.cpp @@ -111,9 +111,9 @@ void DialogProgress::SetProgress(int cur,int max) { else count++; } - wxCommandEvent* evt = new wxCommandEvent(wxEVT_PROGRESS_UPDATE,0); - evt->SetInt(value); - AddPendingEvent(*evt); + wxCommandEvent evt(wxEVT_PROGRESS_UPDATE,0); + evt.SetInt(value); + AddPendingEvent(evt); } diff --git a/aegisub/src/dialog_style_manager.cpp b/aegisub/src/dialog_style_manager.cpp index cddb368d6..3851a38ec 100644 --- a/aegisub/src/dialog_style_manager.cpp +++ b/aegisub/src/dialog_style_manager.cpp @@ -746,8 +746,9 @@ void DialogStyleManager::PasteToCurrent() { wxStringTokenizer st(data,_T('\n')); while (st.HasMoreTokens()) { + AssStyle *s = NULL; try { - AssStyle *s = new AssStyle(st.GetNextToken().Trim(true)); + s = new AssStyle(st.GetNextToken().Trim(true)); if (s->Valid) { while (AssFile::top->GetStyle(s->name) != NULL) s->name = _T("Copy of ") + s->name; @@ -763,6 +764,7 @@ void DialogStyleManager::PasteToCurrent() { wxMessageBox(_("Could not parse style"), _("Could not parse style"), wxOK | wxICON_EXCLAMATION , this); } catch (...) { + delete s; wxMessageBox(_("Could not parse style"), _("Could not parse style"), wxOK | wxICON_EXCLAMATION , this); } @@ -785,8 +787,9 @@ void DialogStyleManager::PasteToStorage() { wxStringTokenizer st(data,_T('\n')); while (st.HasMoreTokens()) { + AssStyle *s = NULL; try { - AssStyle *s = new AssStyle(st.GetNextToken().Trim(true)); + s = new AssStyle(st.GetNextToken().Trim(true)); if (s->Valid) { while (Store.GetStyle(s->name) != NULL) s->name = _T("Copy of ") + s->name; @@ -802,6 +805,7 @@ void DialogStyleManager::PasteToStorage() { wxMessageBox(_("Could not parse style"), _("Could not parse style"), wxOK | wxICON_EXCLAMATION , this); } catch(...) { + delete s; wxMessageBox(_("Could not parse style"), _("Could not parse style"), wxOK | wxICON_EXCLAMATION , this); } diff --git a/aegisub/src/mkv_wrap.cpp b/aegisub/src/mkv_wrap.cpp index f83f70e92..e06b57a4a 100644 --- a/aegisub/src/mkv_wrap.cpp +++ b/aegisub/src/mkv_wrap.cpp @@ -122,7 +122,6 @@ void MatroskaWrapper::Close() { if (file) { mkv_Close(file); file = NULL; - fclose(input->fp); delete input; } keyFrames.Clear(); diff --git a/aegisub/src/mkv_wrap.h b/aegisub/src/mkv_wrap.h index 50c6da85a..512043e5e 100644 --- a/aegisub/src/mkv_wrap.h +++ b/aegisub/src/mkv_wrap.h @@ -65,6 +65,7 @@ class AssFile; class MkvStdIO : public InputStream { public: MkvStdIO(wxString filename); + ~MkvStdIO() { if (fp) fclose(fp); } /// DOCME FILE *fp;