From f3eb3f59e56d6ecb355b00ee0c8d5229e06d858d Mon Sep 17 00:00:00 2001 From: "dahyeong.kim" Date: Thu, 25 Apr 2013 18:52:56 +0900 Subject: [PATCH] Fix MemoryLeak in String class Change-Id: Ife2026af34a0e7956bafac7eaa4b3d162a53aba8 Signed-off-by: dahyeong.kim --- src/base/FBaseString.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/base/FBaseString.cpp b/src/base/FBaseString.cpp index 24dda81..ec9aae1 100644 --- a/src/base/FBaseString.cpp +++ b/src/base/FBaseString.cpp @@ -995,7 +995,7 @@ String::Replace(const String& org, const String& rep, int startIndex) void String::Reverse(void) { - if (*__pRefCount > 0) + if (*__pRefCount > 1) { result r = CopyOnWrite(__capacity); SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed."); @@ -1304,7 +1304,7 @@ String::ToLower(void) void String::ToLowerCase(void) { - if (*__pRefCount > 0) + if (*__pRefCount > 1) { result r = CopyOnWrite(__capacity); SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed."); @@ -1396,14 +1396,26 @@ String::Trim(void) --lastIndex; } - // trim right - if (lastIndex < __length) + bool trimRight = lastIndex < __length; + bool trimLeft = startIndex > 0; + + if (!trimRight && !trimLeft) // nothing to trim + { + return; + } + + if (*__pRefCount > 1) + { + result r = CopyOnWrite(__capacity); + SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed"); + } + + if (trimRight) { Remove(lastIndex, __length - lastIndex); } - // trim left - if (startIndex > 0) + if (trimLeft) { Remove(0, startIndex); } -- 2.7.4