From: Václav Slavík Date: Sun, 8 Apr 2007 17:52:00 +0000 (+0000) Subject: fix crash in ~wxString with global wxString objects: temporarily move conversion... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/132276cf0da4de71d7ba8aeb1ba0d9a2c505a631 fix crash in ~wxString with global wxString objects: temporarily move conversion buffers from a hash to wxString git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45335 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/string.h b/include/wx/string.h index 208f343912..a931a09fae 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -571,8 +571,6 @@ public: wxString(const wxString& str, size_t nLength) : m_impl(str.Mid(0, nLength).m_impl) {} - ~wxString(); - public: // standard types typedef wxUniChar value_type; @@ -2001,6 +1999,35 @@ private: private: wxStringImpl m_impl; + + // buffers for compatibility conversion from (char*)c_str() and + // (wchar_t*)c_str(): + // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers + template + struct ConvertedBuffer + { + ConvertedBuffer() : m_buf(NULL) {} + ~ConvertedBuffer() + { free(m_buf); } + + operator const T*() const { return m_buf; } + + ConvertedBuffer& operator=(T *str) + { + free(m_buf); + m_buf = str; + return *this; + } + + T *m_buf; + }; +#if wxUSE_UNICODE + ConvertedBuffer m_convertedToChar; +#endif +#if !wxUSE_UNICODE_WCHAR + ConvertedBuffer m_convertedToWChar; +#endif + friend class WXDLLIMPEXP_BASE wxCStrData; }; #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN diff --git a/src/common/string.cpp b/src/common/string.cpp index aa045dc5ff..3d171f7656 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -109,6 +109,11 @@ wxSTD ostream& operator<<(wxSTD ostream& os, const wxWCharBuffer& str) // wxCStrData converted strings caching // ---------------------------------------------------------------------------- +// FIXME-UTF8: temporarily disabled because it doesn't work with global +// string objects; re-enable after fixing this bug and benchmarking +// performance to see if using a hash is a good idea at all +#if 0 + // For backward compatibility reasons, it must be possible to assign the value // returned by wxString::c_str() to a char* or wchar_t* variable and work with // it. Returning wxCharBuffer from (const char*)c_str() wouldn't do the trick, @@ -177,14 +182,6 @@ const wchar_t* wxCStrData::AsWChar() const } #endif // !wxUSE_UNICODE_WCHAR -// =========================================================================== -// wxString class core -// =========================================================================== - -// --------------------------------------------------------------------------- -// construction and conversion -// --------------------------------------------------------------------------- - wxString::~wxString() { #if wxUSE_UNICODE @@ -195,6 +192,35 @@ wxString::~wxString() DeleteStringFromConversionCache(gs_stringsWCharCache, this); #endif } +#endif + +#if wxUSE_UNICODE +const char* wxCStrData::AsChar() const +{ + wxString *str = wxConstCast(m_str, wxString); + // convert the string and keep it: + str->m_convertedToChar = str->mb_str().release(); + return str->m_convertedToChar + m_offset; +} +#endif // wxUSE_UNICODE + +#if !wxUSE_UNICODE_WCHAR +const wchar_t* wxCStrData::AsWChar() const +{ + wxString *str = wxConstCast(m_str, wxString); + // convert the string and keep it: + str->m_convertedToWChar = str->wc_str().release(); + return str->m_convertedToWChar + m_offset; +} +#endif // !wxUSE_UNICODE_WCHAR + +// =========================================================================== +// wxString class core +// =========================================================================== + +// --------------------------------------------------------------------------- +// construction and conversion +// --------------------------------------------------------------------------- #if wxUSE_UNICODE /* static */