gabstv / ebiten-imgui

Dear ImGui renderer for Ebitengine
MIT License
122 stars 18 forks source link

Actions on a window's top bar result in a crash [solved] #23

Closed Omustardo closed 1 month ago

Omustardo commented 1 month ago

I ran into this issue but ended up solving it. Posting here anyway since I had it mostly written up, and it may help someone else who's new to imgui someday.


It seems that any window I create crashes if you either:

It doesn't happen for the "Debug" menu that seems to be built in.

I've attached sample code in demo.go.txt (.txt because github doesn't allow .go attachments).

Example triggering the issue: Peek 2024-08-13 18-47

The error is:

File: /home/runner/work/cimgui-go/cimgui-go/cimgui/imgui/imgui.cpp, Line: 9932
exit status 1

That is: https://github.com/gabstv/cimgui-go/blob/main/cimgui/imgui/imgui.cpp#L9932 which states

IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?");

I debugged it a bit and figured out the issue. I was definitely holding it wrong, but it also feels like a footgun.

My code was:

if imgui.Begin("Sample window") {
    imgui.Text("foo")
    imgui.End()
}

The problem is that it was calling imgui.Begin() but not imgui.End(). In retrospect this is obvious, but I had for some reason assumed that the boolean returned by Begin indicated whether it began successfully, and if not then I shouldn't try anything else.

The following works, with indentation to make the UI layout more readable when things start getting nested.

{
    imgui.Begin("Sample window")
    imgui.Text("foo")
    imgui.End()
}