Fix MemoryLeak in String class
[platform/framework/native/appfw.git] / 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);
        }