From e34ad0a05a6481e4748c468a8e6c41cb3de3c8b4 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 3 Sep 2015 23:42:08 +0100 Subject: [PATCH] ImDrawList: fixed 1 leak per ImDrawList using the ChannelsSplit() API (via Columns) (fix #318) --- imgui_draw.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/imgui_draw.cpp b/imgui_draw.cpp index b38d2e75..54eda29a 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -157,12 +157,22 @@ void ImDrawList::ChannelsSplit(int channels_count) if (old_channels_count < channels_count) _Channels.resize(channels_count); _ChannelsCount = channels_count; - for (int i = 0; i < channels_count; i++) + + // _Channels[] (24 bytes each) hold storage that we'll swap with this->_CmdBuffer/_IdxBuffer + // The content of _Channels[0] at this point doesn't matter. We clear it to make state tidy in a debugger but we don't strictly need to. + // When we switch to the next channel, we'll copy _CmdBuffer/_IdxBuffer into _Channels[0] and then _Channels[1] into _CmdBuffer/_IdxBuffer + memset(&_Channels[0], 0, sizeof(ImDrawChannel)); + for (int i = 1; i < channels_count; i++) { if (i >= old_channels_count) + { new(&_Channels[i]) ImDrawChannel(); - else if (i > 0) - _Channels[i].CmdBuffer.resize(0), _Channels[i].IdxBuffer.resize(0); + } + else + { + _Channels[i].CmdBuffer.resize(0); + _Channels[i].IdxBuffer.resize(0); + } if (_Channels[i].CmdBuffer.Size == 0) { ImDrawCmd draw_cmd;