From 4685fdeca38b48b71b870f6d659c7de9694f349c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 21 Feb 2015 21:51:06 -0800 Subject: [PATCH] Fixed dynamic vertex/index buffer leak warning. Issue #263. --- examples/23-vectordisplay/vectordisplay.cpp | 7 +++- src/bgfx_p.h | 36 +++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/examples/23-vectordisplay/vectordisplay.cpp b/examples/23-vectordisplay/vectordisplay.cpp index 202f03d7..1c094cf4 100644 --- a/examples/23-vectordisplay/vectordisplay.cpp +++ b/examples/23-vectordisplay/vectordisplay.cpp @@ -123,6 +123,11 @@ void VectorDisplay::resize(uint16_t _width, uint16_t _height) void VectorDisplay::teardown() { + for (size_t i = 0; i < m_vertexBuffers.size(); ++i) + { + bgfx::destroyDynamicVertexBuffer(m_vertexBuffers[i]); + } + teardownResDependent(); bgfx::destroyProgram(m_drawToScreenShader); @@ -728,7 +733,7 @@ bool VectorDisplay::setDecaySteps(int _steps) { for (size_t i = 0; i < m_vertexBuffers.size(); ++i) { - destroyDynamicVertexBuffer(m_vertexBuffers[i]); + bgfx::destroyDynamicVertexBuffer(m_vertexBuffers[i]); } m_vertexBuffers.clear(); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index cfc32cf8..f0b37533 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1738,6 +1738,20 @@ namespace bgfx m_free.push_back(Free(_ptr, _size) ); } + uint64_t remove() + { + BX_CHECK(0 == m_used.size(), ""); + + if (0 < m_free.size() ) + { + Free free = m_free.front(); + m_free.pop_front(); + return free.m_ptr; + } + + return 0; + } + uint64_t alloc(uint32_t _size) { for (FreeList::iterator it = m_free.begin(), itEnd = m_free.end(); it != itEnd; ++it) @@ -1776,7 +1790,7 @@ namespace bgfx } } - void compact() + bool compact() { m_free.sort(); @@ -1793,6 +1807,8 @@ namespace bgfx ++next; } } + + return 0 == m_used.size(); } private: @@ -2165,7 +2181,14 @@ namespace bgfx else { m_dynIndexBufferAllocator.free(uint64_t(dib.m_handle.idx)<<32 | dib.m_offset); - m_dynIndexBufferAllocator.compact(); + if (m_dynIndexBufferAllocator.compact() ) + { + for (uint64_t ptr = m_dynIndexBufferAllocator.remove(); 0 != ptr; ptr = m_dynIndexBufferAllocator.remove() ) + { + IndexBufferHandle handle = { uint16_t(ptr>>32) }; + destroyIndexBuffer(handle); + } + } } m_dynamicIndexBufferHandle.free(_handle.idx); @@ -2310,7 +2333,14 @@ namespace bgfx else { m_dynVertexBufferAllocator.free(uint64_t(dvb.m_handle.idx)<<32 | dvb.m_offset); - m_dynVertexBufferAllocator.compact(); + if (m_dynVertexBufferAllocator.compact() ) + { + for (uint64_t ptr = m_dynVertexBufferAllocator.remove(); 0 != ptr; ptr = m_dynVertexBufferAllocator.remove() ) + { + VertexBufferHandle handle = { uint16_t(ptr>>32) }; + destroyVertexBuffer(handle); + } + } } m_dynamicVertexBufferHandle.free(_handle.idx);