Skip to content

Commit

Permalink
Fixed memory leak due to command buffers allocated with Application::…
Browse files Browse the repository at this point in the history
…GetCommandBuffer never being freed
  • Loading branch information
TheCherno committed Apr 25, 2022
1 parent d64a0d0 commit 1f18ebb
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Walnut/src/Walnut/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ static ImGui_ImplVulkanH_Window g_MainWindowData;
static int g_MinImageCount = 2;
static bool g_SwapChainRebuild = false;

static std::vector<std::vector<VkCommandBuffer>> s_AllocatedCommandBuffers;

void check_vk_result(VkResult err)
{
if (err == 0)
Expand Down Expand Up @@ -282,6 +284,14 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data)
check_vk_result(err);
}
{
// Free command buffers allocated by Application::GetCommandBuffer
auto& allocatedCommandBuffers = s_AllocatedCommandBuffers[wd->FrameIndex];
if (allocatedCommandBuffers.size() > 0)
{
vkFreeCommandBuffers(g_Device, fd->CommandPool, (uint32_t)allocatedCommandBuffers.size(), allocatedCommandBuffers.data());
allocatedCommandBuffers.clear();
}

err = vkResetCommandPool(g_Device, fd->CommandPool, 0);
check_vk_result(err);
VkCommandBufferBeginInfo info = {};
Expand Down Expand Up @@ -400,6 +410,8 @@ namespace Walnut {
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
SetupVulkanWindow(wd, surface, w, h);

s_AllocatedCommandBuffers.resize(wd->ImageCount);

// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
Expand Down Expand Up @@ -653,7 +665,7 @@ namespace Walnut {
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufAllocateInfo.commandBufferCount = 1;

VkCommandBuffer command_buffer;
VkCommandBuffer& command_buffer = s_AllocatedCommandBuffers[wd->FrameIndex].emplace_back();
auto err = vkAllocateCommandBuffers(g_Device, &cmdBufAllocateInfo, &command_buffer);

VkCommandBufferBeginInfo begin_info = {};
Expand Down

0 comments on commit 1f18ebb

Please sign in to comment.