Fix for double deletion of memory pool objects 81/65181/5
authorTom Robinson <tom.robinson@samsung.com>
Thu, 7 Apr 2016 16:52:55 +0000 (17:52 +0100)
committerTom Robinson <tom.robinson@samsung.com>
Fri, 8 Apr 2016 11:56:52 +0000 (12:56 +0100)
Change-Id: I6bd80062f4884a220880af397264da027974967b

automated-tests/src/dali-internal/utc-Dali-Internal-MemoryPoolObjectAllocator.cpp
dali/internal/common/memory-pool-object-allocator.h
dali/internal/event/size-negotiation/memory-pool-relayout-container.cpp

index e254b68..7355c60 100644 (file)
@@ -117,7 +117,7 @@ int UtcDaliMemoryPoolObjectAllocatorObjectAllocation(void)
   testObject1->DataAccess();
   DALI_TEST_EQUALS( tracking1.testObjectDataAccess, 1, TEST_LOCATION );
 
-  allocator.Free( testObject1 );
+  allocator.Destroy( testObject1 );
   DALI_TEST_EQUALS( tracking1.testObjectDestructed, 1, TEST_LOCATION );
 
   // Reset and allocate another object
@@ -135,7 +135,7 @@ int UtcDaliMemoryPoolObjectAllocatorObjectAllocation(void)
   testObject2->DataAccess();
   DALI_TEST_EQUALS( tracking2.testObjectDataAccess, 1, TEST_LOCATION );
 
-  allocator.Free( testObject2 );
+  allocator.Destroy( testObject2 );
   DALI_TEST_EQUALS( tracking2.testObjectDestructed, 1, TEST_LOCATION );
 
   END_TEST;
@@ -157,7 +157,7 @@ int UtcDaliMemoryPoolObjectAllocatorObjectRawAllocation(void)
   testObject->DataAccess();
   DALI_TEST_EQUALS( tracking.testObjectDataAccess, 1, TEST_LOCATION );
 
-  allocator.Free( testObject );
+  allocator.Destroy( testObject );
   DALI_TEST_EQUALS( tracking.testObjectDestructed, 1, TEST_LOCATION );
 
   END_TEST;
@@ -170,14 +170,14 @@ int UtcDaliMemoryPoolObjectAllocatorObjectAllocationPOD(void)
   bool* testObject1 = allocator.Allocate();
   DALI_TEST_CHECK( testObject1 );
 
-  allocator.Free( testObject1 );
+  allocator.Destroy( testObject1 );
 
   allocator.ResetMemoryPool();
 
   bool* testObject2 = allocator.Allocate();
   DALI_TEST_CHECK( testObject2 );
 
-  allocator.Free( testObject2 );
+  allocator.Destroy( testObject2 );
 
   END_TEST;
 }
index d38aa7d..93e1d18 100644 (file)
@@ -2,7 +2,7 @@
 #define __DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H__
 
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -101,25 +101,49 @@ public:
 
   /**
    * @brief Return the object to the memory pool
+   * Note: This performs a deallocation only, if the object has a destructor and is not
+   * freed from within an overloaded delete operator, Destroy() must be used instead.
    *
    * @param object Pointer to the object to delete
    */
   void Free( T* object )
   {
-    object->~T();
-
     mPool->Free( object );
   }
 
   /**
    * @brief Thread-safe version of Free()
+   * Note: This performs a deallocation only, if the object has a destructor and is not
+   * freed from within an overloaded delete operator, DestroyThreadSafe() must be used instead.
    *
    * @param object Pointer to the object to delete
    */
   void FreeThreadSafe( T* object )
   {
+    mPool->FreeThreadSafe( object );
+  }
+
+  /**
+   * @brief Return the object to the memory pool after destructing it.
+   * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
+   *
+   * @param object Pointer to the object to delete
+   */
+  void Destroy( T* object )
+  {
     object->~T();
+    mPool->Free( object );
+  }
 
+  /**
+   * @brief Thread-safe version of Destroy()
+   * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
+   *
+   * @param object Pointer to the object to delete
+   */
+  void DestroyThreadSafe( T* object )
+  {
+    object->~T();
     mPool->FreeThreadSafe( object );
   }
 
@@ -128,10 +152,7 @@ public:
    */
   void ResetMemoryPool()
   {
-    if( mPool )
-    {
-      delete mPool;
-    }
+    delete mPool;
 
     mPool = new FixedSizeMemoryPool( TypeSizeWithAlignment< T >::size );
   }
index 617f801..e71af13 100644 (file)
@@ -67,7 +67,7 @@ void MemoryPoolRelayoutContainer::PopBack()
     RelayoutInfoContainer::Iterator back = mRelayoutInfos.End();
     back--;
     RelayoutInfo* info = *back;
-    mAllocator.Free( info );
+    mAllocator.Destroy( info );
     mRelayoutInfos.Erase( back );
   }
 }
@@ -96,7 +96,7 @@ void MemoryPoolRelayoutContainer::Clear()
   for( size_t i = 0; i < Size(); ++i )
   {
     RelayoutInfo* info = mRelayoutInfos[ i ];
-    mAllocator.Free( info );
+    mAllocator.Destroy( info );
   }
 
   mRelayoutInfos.Clear();