From a4cc201e76424a10b6235d8034b49c8739d98b81 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 21 Feb 2014 00:51:43 +0000 Subject: [PATCH] Fix memory leak on error return from wxMsgCatalogFile::FillHash(). Use wxScopedPtr to make memory management simpler and to ensure that all pointers allocated in this function are deleted: this wasn't the case when we returned false earlier due to the MO file being invalid. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75954 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/translation.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/common/translation.cpp b/src/common/translation.cpp index f43638a1f5..ed72d802c6 100644 --- a/src/common/translation.cpp +++ b/src/common/translation.cpp @@ -46,6 +46,7 @@ #include "wx/filename.h" #include "wx/tokenzr.h" #include "wx/fontmap.h" +#include "wx/scopedptr.h" #include "wx/stdpaths.h" #include "wx/private/threadinfo.h" @@ -1188,7 +1189,8 @@ bool wxMsgCatalogFile::FillHash(wxStringToStringHashMap& hash, // conversion to use to convert catalog strings to the GUI encoding wxMBConv *inputConv = NULL; - wxMBConv *inputConvPtr = NULL; // same as inputConv but safely deleteable + + wxScopedPtr inputConvPtr; // just to delete inputConv if needed if ( !m_charset.empty() ) { @@ -1198,8 +1200,11 @@ bool wxMsgCatalogFile::FillHash(wxStringToStringHashMap& hash, if ( encCat != wxLocale::GetSystemEncoding() ) #endif { - inputConvPtr = inputConv = new wxCSConv(m_charset); + + // As we allocated it ourselves, we need to delete it, so ensure + // this happens. + inputConvPtr.reset(inputConv); } } else // no need or not possible to convert the encoding @@ -1217,9 +1222,9 @@ bool wxMsgCatalogFile::FillHash(wxStringToStringHashMap& hash, // conversion to apply to msgid strings before looking them up: we only // need it if the msgids are neither in 7 bit ASCII nor in the same // encoding as the catalog - wxCSConv *sourceConv = msgIdCharset.empty() || (msgIdCharset == m_charset) - ? NULL - : new wxCSConv(msgIdCharset); + wxScopedPtr sourceConv; + if ( !msgIdCharset.empty() && (msgIdCharset != m_charset) ) + sourceConv.reset(new wxCSConv(msgIdCharset)); #endif // !wxUSE_UNICODE for (size_t32 i = 0; i < m_numStrings; i++) @@ -1275,11 +1280,6 @@ bool wxMsgCatalogFile::FillHash(wxStringToStringHashMap& hash, } } -#if !wxUSE_UNICODE - delete sourceConv; -#endif - delete inputConvPtr; - return true; }