Procházet zdrojové kódy

Release memory on dtor call

Daniele Bartolini před 12 roky
rodič
revize
680e070340

+ 8 - 3
engine/core/mem/LinearAllocator.cpp

@@ -31,21 +31,26 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 LinearAllocator::LinearAllocator(Allocator& backing, size_t size)
-	: m_physical_start(NULL), m_total_size(size), m_offset(0)
+	: m_backing(&backing), m_physical_start(NULL), m_total_size(size), m_offset(0)
 {
 	m_physical_start = backing.allocate(size);
 }
 
 //-----------------------------------------------------------------------------
 LinearAllocator::LinearAllocator(void* start, size_t size)
-	: m_physical_start(start), m_total_size(size), m_offset(0)
+	: m_backing(NULL), m_physical_start(start), m_total_size(size), m_offset(0)
 {
 }
 
 //-----------------------------------------------------------------------------
 LinearAllocator::~LinearAllocator()
 {
-	CE_ASSERT(m_offset == 0, "Memory leak of %d bytes", m_offset);
+	if (m_backing)
+	{
+		m_backing->deallocate(m_physical_start);
+	}
+
+	CE_ASSERT(m_offset == 0, "Memory leak of %d bytes, maybe you forgot to call clear()?", m_offset);
 }
 
 //-----------------------------------------------------------------------------

+ 1 - 0
engine/core/mem/LinearAllocator.h

@@ -59,6 +59,7 @@ public:
 
 private:
 
+	Allocator*	m_backing;
 	void*		m_physical_start;
 	size_t		m_total_size;
 	size_t		m_offset;