ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
60.61k stars 10.23k forks source link

Exception thrown at 0x????... after close program, maybe bad initialization/use of ImDrawList object? #4422

Closed mandaxyz closed 3 years ago

mandaxyz commented 3 years ago

My code seems to work and it able to display the rectangles which means that it's almost ok, but when I close the program with the "close" button, then an error "Exception thrown at 0x????..." and it breaks on my custom FREE callback and tells that it found the "memory block" but the 'size' is 0 and the 'pointer' is not null, and it was allocated with an operator 'new' by a Vulkan allocation callback, also sometimes it doesn't crash (75% of case), and the "random crash" means that "There may be a problem on the initialization/use of ImDrawList object".

Version: dear imgui, v1.65 (because it's used by the current Vulkan C++ examples and demos)

Here is an algorithm explanation code, it cannot be reproduced because it requires Vulkan and many other stuff.

// Initialization (How to properly initialize?)
this->_drawList = new ImDrawList(&_drawListSharedData);
this->_drawList->AddDrawCmd(); // Here is my biggest question, is this method correct? Without this line, nothing works.
// <-- What line should I add here? -->

The code bellow is called just before multiple (maybe 3) calls of _shader->bind(commandBuffer); _batch.draw(commandBuffer);, where batch is the Vertex Buffer & Index Buffer.

// The code below is called "Update Buffers"
_drawList->clear(); // Without this line (clear), there is never a crash, note that it's exactly like ImDrawList::Clear().
// <-- What line should I add here? A problem about clearing -->
for (int i = 0; i < 10; i++)
{
    // The line below is exactly like ImDrawList::AddRect(...)
    _drawList->addRect(vec2(10 + i * 15, 20 + i * 15), vec2(100 + i * 15, 120 + i * 15), 0xFFFFFFFF);
}
// The line below is not an ImGui function, it's my engine function using Vulkan. The bug is not here because without the 'clear' there is no crash, 
_drawList->updateBuffers(copyQueue); // Updated when required.

Have anyone experienced the same issue? Because I cannot see where is the problem, my intuition tells me that "There maybe problem on my ImDrawList initialization/use". Any help please? I'm sure my custom memory managment has no bug because I used it for a long time without problem.

Can anyone tell me that I must update to the latest Dear ImGui? Nobody knows if the bug will gone if I do it, also it requires a lot of work because the Vulkan demo stucks on that old ImGui (Version from 3 years ago).

mandaxyz commented 3 years ago

Solved, I realize that every time I write an explanatory comment on a Forum then I see many details, and here is the solution:

_drawList->clear();
_drawList->_drawList->AddDrawCmd(); // Add this line because the clear function removes all draw commands.

I'm proud of my self that I have a spirit of developer.

ocornut commented 3 years ago

Good to hear! (Might have been difficult for us to provide help for a version as old as 1.65, i would advise to update!)

Interesting read about this common effect: https://en.m.wikipedia.org/wiki/Rubber_duck_debugging