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
This commit is contained in:
Václav Slavík
2007-04-08 17:52:00 +00:00
parent 7708a26720
commit 132276cf0d
2 changed files with 63 additions and 10 deletions

View File

@@ -571,8 +571,6 @@ public:
wxString(const wxString& str, size_t nLength) wxString(const wxString& str, size_t nLength)
: m_impl(str.Mid(0, nLength).m_impl) {} : m_impl(str.Mid(0, nLength).m_impl) {}
~wxString();
public: public:
// standard types // standard types
typedef wxUniChar value_type; typedef wxUniChar value_type;
@@ -2001,6 +1999,35 @@ private:
private: private:
wxStringImpl m_impl; 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<typename T>
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<char> m_convertedToChar;
#endif
#if !wxUSE_UNICODE_WCHAR
ConvertedBuffer<wchar_t> m_convertedToWChar;
#endif
friend class WXDLLIMPEXP_BASE wxCStrData;
}; };
#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN

View File

@@ -109,6 +109,11 @@ wxSTD ostream& operator<<(wxSTD ostream& os, const wxWCharBuffer& str)
// wxCStrData converted strings caching // 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 // 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 // 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, // 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 #endif // !wxUSE_UNICODE_WCHAR
// ===========================================================================
// wxString class core
// ===========================================================================
// ---------------------------------------------------------------------------
// construction and conversion
// ---------------------------------------------------------------------------
wxString::~wxString() wxString::~wxString()
{ {
#if wxUSE_UNICODE #if wxUSE_UNICODE
@@ -195,6 +192,35 @@ wxString::~wxString()
DeleteStringFromConversionCache(gs_stringsWCharCache, this); DeleteStringFromConversionCache(gs_stringsWCharCache, this);
#endif #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 #if wxUSE_UNICODE
/* static */ /* static */