Fix MemoryLeak in String class
authordahyeong.kim <dahyeong.kim@samsung.com>
Thu, 25 Apr 2013 09:52:56 +0000 (18:52 +0900)
committerdahyeong.kim <dahyeong.kim@samsung.com>
Thu, 25 Apr 2013 09:52:56 +0000 (18:52 +0900)
Change-Id: Ife2026af34a0e7956bafac7eaa4b3d162a53aba8
Signed-off-by: dahyeong.kim <dahyeong.kim@samsung.com>
src/base/FBaseString.cpp

index 24dda81..ec9aae1 100644 (file)
@@ -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);
        }