Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help! Program crashes after ImGui panel is dragged slightly off screen #7414

Closed
kaitabuchi314 opened this issue Mar 17, 2024 · 2 comments
Closed

Comments

@kaitabuchi314
Copy link

Version/Branch of Dear ImGui:

docking

Back-ends:

imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp

Compiler, OS:

Windows 10

Full config/build information:

No response

Details:

My ImGui app crashes after I drag a panel offscreen. It says Draw_data is nullptr.

Screenshots/Video:

Screenshot 2024-03-17 133931

Minimal, Complete and Verifiable Example code:

ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();
        ImGui::DockSpaceOverViewport(ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode);
        renderer.ClearScreen(0.3f, 0.6f, 0.2f);

        renderer.DrawImage(img, x, y);

        ImGui::Begin("Settings");
        ImGui::Text("Hello, ImGui!");
        ImGui::End();
        ImGui::EndFrame();
        ImGui::UpdatePlatformWindows();
        ImGui::RenderPlatformWindowsDefault();
        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
@PathogenDavid
Copy link
Contributor

Hello!

In the future you should include a stack trace from the crash, otherwise there usually isn't enough info to help you debug. It's shown in the call stack window of Visual Studio when the debugger stops your program. It'll look something like this:

image

(If you don't see this window, go to Debug > Windows > Call Stack)


That being said, your render code isn't quite right. ImGui::UpdatePlatformWindows and ImGui::RenderPlatformWindowsDefault must happen after ImGui::Render.

You're getting a crash because you're trying to render platform windows before ImGui::Render is called to create the draw data necessary to render them.

Additionally you're missing the logic for backing up and restoring the OpenGL context, see the example_glfw_opengl3 for reference:

// Update and Render additional Platform Windows
// (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
// For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}

@ocornut
Copy link
Owner

ocornut commented Apr 25, 2024

Thank you David for your answer.

This error would normally be caught in UpdatePlatformWindows() as there is a corresponding assert which will detect it:

IM_ASSERT(g.FrameCountEnded == g.FrameCount && "Forgot to call Render() or EndFrame() before UpdatePlatformWindows()?");
IM_ASSERT(g.FrameCountPlatformEnded < g.FrameCount);

So it means @kaitabuchi314 that your assert are disabled, and me adding more checks/asserts won't help.

To facilitate your use of dear imgui you should make sure that failing asserts are visible to do.
E.g. if you call IM_ASSERT(false); your app/debugger should stop or show you a message.

By default IM_ASSERT() calls assert() and I don't exactly know what would make assert disabled but it is generally when NDEBUG is defined via the build system. I believe that cmake by default sets NDEBUG=true which IMHO is a huge problem, this should be overridden.

Good luck!

@ocornut ocornut closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants