Skip to content

nullpointer when shutting down multi window context #8676

@ThunderSphun

Description

@ThunderSphun

Version/Branch of Dear ImGui:

Version 1.92.0, Branch: docking

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

Windows 11 + MSVC 2022

Full config/build information:

Dear ImGui 1.92.0 WIP (19197)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_glfw (3500)
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000481
 NavEnableKeyboard
 DockingEnable
 ViewportsEnable
io.ConfigViewportsNoDecoration
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 640.00,480.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

I am trying to create multiple glfw windows, each with their own imgui context to make imgui work with multiple windows.
one of the problems that i found is that if i close one window, everything works fine, but when i close the other window, there is a null pointer error
it happens in this line

callstack of the example program i have attached:

>	tmp.exe!ImGui_ImplGlfw_WndProc(HWND__ * hWnd, unsigned int msg, unsigned __int64 wParam, __int64 lParam) Line 1447	C++
 	user32.dll!00007ffe03fee83a()	Unknown
 	user32.dll!00007ffe03fee11c()	Unknown
 	user32.dll!00007ffe0402a516()	Unknown
 	ntdll.dll!00007ffe06385f94()	Unknown
 	win32u.dll!00007ffe03fb2534()	Unknown
 	tmp.exe!_glfwDestroyWindowWin32(_GLFWwindow * window) Line 1575	C
 	tmp.exe!glfwDestroyWindow(GLFWwindow * handle) Line 486	C
 	tmp.exe!main() Line 49	C++

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

#include <imgui.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#include <GLFW/glfw3.h>

int main() {
	if (!glfwInit())
		return -1;

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

	struct windowContext {
		GLFWwindow* window;
		ImGuiContext* context;
	};

	windowContext windows[2] = {};

	for (int i = 0; i < 2; i++) {
		windows[i].window = glfwCreateWindow(640, 480, "title", nullptr, nullptr);
		if (windows[i].window == nullptr) {
			glfwTerminate();
			return -1;
		}

		glfwMakeContextCurrent(windows[i].window);
		glfwSwapInterval(1);

		windows[i].context = ImGui::CreateContext();
		ImGui::SetCurrentContext(windows[i].context);
		ImGuiIO& io = ImGui::GetIO();
		io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
		io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
		io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;

		ImGui::StyleColorsDark();

		ImGui_ImplGlfw_InitForOpenGL(windows[i].window, false);
		ImGui_ImplOpenGL3_Init("#version 430");
	}

	//for (int i = 0; i < 2; i++) { // works
	for (int i = 1; i >= 0; i--) { // doesn't work
		ImGui::SetCurrentContext(windows[i].context);
		ImGui_ImplOpenGL3_Shutdown();
		ImGui_ImplGlfw_Shutdown();

		glfwDestroyWindow(windows[i].window);
	}

	glfwTerminate();

	return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions