Modified memory leak of EnrichedText
authorYunji Park <yunji19.park@samsung.com>
Mon, 6 May 2013 06:04:37 +0000 (15:04 +0900)
committerYunji Park <yunji19.park@samsung.com>
Mon, 6 May 2013 06:04:37 +0000 (15:04 +0900)
Change-Id: I864b95dd294e10eddf1e9bd55bc7cce1b34fb3c1

src/graphics/FGrp_EnrichedTextImpl.cpp
src/graphics/FGrp_TextElementImpl.cpp
src/graphics/inc/FGrp_TextElementImpl.h

index 1e27445..7cf0e9f 100644 (file)
@@ -51,7 +51,6 @@ _EnrichedTextImpl::_EnrichedTextImpl(void)
        __pLinkedListTextElementList = null;
        __pTextElement = null;
        __isCopiedEnrichedText = false;
-       __pLinkedListTextElementList = null;
        __pTextElement = null;
 }
 
@@ -201,11 +200,6 @@ _EnrichedTextImpl::RemoveAt(int elementIndex, bool deallocate)
                        , E_INVALID_ARG, E_INVALID_ARG
                        , "[E_INVALID_ARG] The argument is invalid.");
 
-       if (__pTextObject.use_count() > 1)
-       {
-               return E_SUCCESS;
-       }
-
        result r = E_SUCCESS;
        int count = GetTextElementCount();
        TextElement* pTextElement = null;
@@ -250,11 +244,6 @@ _EnrichedTextImpl::Remove(Tizen::Graphics::TextElement& element, bool deallocate
        SysTryReturn(NID_GRP, __pTextObject != null, E_OPERATION_FAILED, E_OPERATION_FAILED
                        , "[E_OPERATION_FAILED] This instance is not constructed yet.");
 
-       if (__pTextObject.use_count() > 1)
-       {
-               return E_SUCCESS;
-       }
-
        int textElementIndex = 0;
        result r = E_SUCCESS;
        TextElement* pTextElement = null;
@@ -1115,9 +1104,7 @@ _EnrichedTextImpl::InsertAt(int elementIndex, const Tizen::Graphics::Bitmap& bit
        }
 
        r = pNewBitmap->Construct(*pBitmap, FloatRectangle(0, 0, pBitmap->GetWidthF(), pBitmap->GetHeightF()));
-       SysTryCatch(NID_GRP
-                       , r == E_SUCCESS
-                       , , r, "[%s] Fails to insert image.", GetErrorMessage(r));
+       SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Fails to insert image.", GetErrorMessage(r));
 
        for (int i = 0; i < elementIndex; i++)
        {
@@ -1132,9 +1119,7 @@ _EnrichedTextImpl::InsertAt(int elementIndex, const Tizen::Graphics::Bitmap& bit
        __pTextObject->Compose();
 
        r = __pLinkedListTextElementList->InsertAt(*pNewBitmap, elementIndex);
-       SysTryCatch(NID_GRP
-                       , r == E_SUCCESS
-                       , , r, "[%s] Fails to insert image.", GetErrorMessage(r));
+       SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Fails to insert image.", GetErrorMessage(r));
 
        if (__isCopiedEnrichedText == false)
        {
@@ -1583,9 +1568,7 @@ _EnrichedTextImpl::GetCloneN(void) const
 
        pNewEnrichedTextImpl = new (std::nothrow)_EnrichedTextImpl(*this);
        r = GetLastResult();
-       SysTryReturn(NID_GRP
-               , pNewEnrichedTextImpl
-               , null, r, "[%s] Propagating.", GetErrorMessage(r));
+       SysTryReturn(NID_GRP, pNewEnrichedTextImpl, null, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return pNewEnrichedTextImpl;
 }
@@ -1633,6 +1616,69 @@ _EnrichedTextImpl::_EnrichedTextImpl(const _EnrichedTextImpl& rhs)
 
                SysLog(NID_GRP, "[%s] Fail to construct text element.", GetErrorMessage(r));
        }
+
+
+       TextElement* pTextElement = null;
+       for (int i = 0; i < __pLinkedListTextElementList->GetCount(); i++)
+       {
+               pTextElement = dynamic_cast < TextElement* >(__pLinkedListTextElementList->GetAt(i));
+               if (pTextElement == null) //Bitmap
+               {
+                       Bitmap* pBitmap = dynamic_cast < Bitmap* >(pTextElement);
+                       SysTryCatch(NID_GRP, pBitmap, , r, "[%s] Fails to get a bitmap.", GetErrorMessage(r));
+
+                       Bitmap* pNewBitmap = new (std::nothrow) Bitmap();
+                       SysTryCatch(NID_GRP, pNewBitmap, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] Fail to create a bitmap.");
+
+                       r = pNewBitmap->Construct(*pBitmap, FloatRectangle(0, 0, pBitmap->GetWidthF(), pBitmap->GetHeightF()));
+                       SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Fails to insert image.", GetErrorMessage(r));
+
+                       r = __pLinkedListTextElementList->InsertAt(*pNewBitmap, i);
+                       SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Fails to insert image.", GetErrorMessage(r));
+               }
+               else
+               {
+                       TextElement* pNewTextElement = _TextElementImpl::CreateTextElementN(*pTextElement);
+                       if (pNewTextElement == null)
+                       {
+                               int i = 0;
+                               i++;
+                       }
+                       SysTryCatch(NID_GRP, pNewTextElement, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] Fail to create a TextElement.");
+
+                       _TextElementImpl* pTextElementImpl = _TextElementImpl::GetInstance(*pNewTextElement);
+                       if (pTextElementImpl == null)
+                       {
+                               int i = 0;
+                               i++;
+                       }
+                       pTextElementImpl->SetRegisteringEnrichedText(this);
+
+                       r = __pLinkedListTextElementList->InsertAt(*pNewTextElement, i);
+                       if (__pLinkedListTextElementList == null)
+                       {
+                               int i = 0;
+                               i++;
+                       }
+                       SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Fails to insert TextElement.", GetErrorMessage(r));
+               }
+       }
+
+       return;
+
+CATCH:
+       if (__pLinkedListTextElementList)
+       {
+               __pLinkedListTextElementList->RemoveAll(true);
+               delete __pLinkedListTextElementList;
+               __pLinkedListTextElementList = null;
+       }
+
+       if (__pTextElement)
+       {
+               delete __pTextElement;
+               __pTextElement = null;
+       }
 }
 
 _EnrichedTextImpl*
index 2c0124c..e7b89b3 100644 (file)
@@ -63,6 +63,7 @@ _TextElementImpl::_TextElementImpl(void)
        __count = 0;
        __linkOffset = 0;
        __pTextComponent = null;
+       __refCount = 1;
 }
 
 _TextElementImpl::~_TextElementImpl(void)
@@ -1089,6 +1090,43 @@ _TextElementImpl::IsOutlineColorEnable(void) const
        return __outlineColorEnable;
 }
 
+void
+_TextElementImpl::AddRef(void)
+{
+       __refCount++;
+}
+
+void
+_TextElementImpl::Release(void)
+{
+       if (__refCount >= 1)
+       {
+               __refCount--;
+               if (__refCount == 0)
+               {
+                       delete this;
+               }
+       }
+}
+
+TextElement*
+_TextElementImpl::CreateTextElementN(TextElement& textElement)
+{
+       TextElement* pTextElement = new (std::nothrow) TextElement;
+       SysTryReturn(NID_GRP, pTextElement, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
+
+       pTextElement->__pImpl = textElement.__pImpl;
+       pTextElement->__pImpl->AddRef();
+
+       return pTextElement;
+}
+
+int
+_TextElementImpl::GetReferenceCount(void) const
+{
+       return __refCount;
+}
+
 _TextElementImpl*
 _TextElementImpl::GetInstance(TextElement& element)
 {
index f3f8ddb..0ad1230 100644 (file)
@@ -100,6 +100,11 @@ public:
        bool IsOutlineColorEnable(void) const;
        bool IsConstructed(void) const;
 
+       static TextElement* CreateTextElementN(TextElement& textElement);
+       void AddRef(void);
+       void Release(void);
+       int GetReferenceCount(void) const;
+
        static _TextElementImpl* GetInstance(TextElement& element);
        static const _TextElementImpl* GetInstance(const TextElement& element);
 
@@ -124,6 +129,8 @@ private:
        int __linkOffset;
        Tizen::Graphics::_Text::TextElement* __pTextComponent;
 
+       int __refCount;
+
 private:
        _TextElementImpl(const _TextElementImpl& textElementImpl);
        _TextElementImpl& operator =(const _TextElementImpl& rhs);