Browse Source

Add leak detection for some elements in debug mode

Michael Ragazzon 6 years ago
parent
commit
f0c898ce04
3 changed files with 17 additions and 2 deletions
  1. 1 0
      Include/RmlUi/Core/ElementInstancer.h
  2. 14 0
      Source/Core/ElementInstancer.cpp
  3. 2 2
      Source/Core/Pool.h

+ 1 - 0
Include/RmlUi/Core/ElementInstancer.h

@@ -83,6 +83,7 @@ class RMLUICORE_API ElementInstancerElement : public ElementInstancer
 public:
 public:
 	ElementPtr InstanceElement(Element* parent, const String& tag, const XMLAttributes& attributes) override;
 	ElementPtr InstanceElement(Element* parent, const String& tag, const XMLAttributes& attributes) override;
 	void ReleaseElement(Element* element) override;
 	void ReleaseElement(Element* element) override;
+	~ElementInstancerElement();
 };
 };
 
 
 /**
 /**

+ 14 - 0
Source/Core/ElementInstancer.cpp

@@ -54,6 +54,20 @@ void ElementInstancerElement::ReleaseElement(Element* element)
 	pool_element.DestroyAndDeallocate(element);
 	pool_element.DestroyAndDeallocate(element);
 }
 }
 
 
+ElementInstancerElement::~ElementInstancerElement()
+{
+	int num_elements = pool_element.GetNumAllocatedObjects();
+	if (num_elements > 0)
+	{
+		Log::Message(Log::LT_WARNING, "--- Found %d leaked element(s) ---", num_elements);
+
+		for (auto it = pool_element.Begin(); it; ++it)
+			Log::Message(Log::LT_WARNING, "    %s", it->GetAddress().c_str());
+
+		Log::Message(Log::LT_WARNING, "------");
+	}
+}
+
 ElementPtr ElementInstancerTextDefault::InstanceElement(Element* /*parent*/, const String& tag, const XMLAttributes& /*attributes*/)
 ElementPtr ElementInstancerTextDefault::InstanceElement(Element* /*parent*/, const String& tag, const XMLAttributes& /*attributes*/)
 {
 {
 	ElementTextDefault* ptr = pool_text_default.AllocateAndConstruct(tag);
 	ElementTextDefault* ptr = pool_text_default.AllocateAndConstruct(tag);

+ 2 - 2
Source/Core/Pool.h

@@ -86,13 +86,13 @@ public:
 		/// node.
 		/// node.
 		inline PoolType& operator*()
 		inline PoolType& operator*()
 		{
 		{
-			return node->object;
+			return *reinterpret_cast<PoolType*>(node->object);
 		}
 		}
 		/// Returns a pointer to the object referenced by the
 		/// Returns a pointer to the object referenced by the
 		/// iterator's current node.
 		/// iterator's current node.
 		inline PoolType* operator->()
 		inline PoolType* operator->()
 		{
 		{
-			return &node->object;
+			return reinterpret_cast<PoolType*>(node->object);
 		}
 		}
 
 
 	private:
 	private: