Index: core/fxcrt/fx_basic_wstring.cpp |
diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp |
index f3b430c7bff3d2d7fc78babc66e2f7e55e20adab..38fd15e13fca4e22237511af7737fb35089adb3d 100644 |
--- a/core/fxcrt/fx_basic_wstring.cpp |
+++ b/core/fxcrt/fx_basic_wstring.cpp |
@@ -224,20 +224,25 @@ void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) { |
if (nNewLength == -1) |
nNewLength = FXSYS_wcslen(m_pData->m_String); |
+ nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); |
if (nNewLength == 0) { |
clear(); |
return; |
} |
- FXSYS_assert(nNewLength <= m_pData->m_nAllocLength); |
- ReallocBeforeWrite(nNewLength); |
+ FXSYS_assert(m_pData->m_nRefs == 1); |
m_pData->m_nDataLength = nNewLength; |
m_pData->m_String[nNewLength] = 0; |
+ if (m_pData->m_nAllocLength - nNewLength >= 32) { |
+ // Over arbitrary threshold, so pay the price to relocate. Force copy to |
+ // always occur by holding a second reference to the string. |
+ CFX_WideString preserve(*this); |
+ ReallocBeforeWrite(nNewLength); |
+ } |
} |
void CFX_WideString::Reserve(FX_STRSIZE len) { |
GetBuffer(len); |
- ReleaseBuffer(GetLength()); |
} |
FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { |