mosra / magnum-integration

Integration libraries for the Magnum C++11 graphics engine
https://magnum.graphics/
Other
97 stars 44 forks source link

Crash in magnum-imgui Example Program #104

Closed WhizZest closed 3 weeks ago

WhizZest commented 8 months ago

I have successfully built and compiled the magnum-imgui example program, but it crashes upon execution, and I have not modified any of the source code. The crash information is "Unhandled exception thrown: read access violation. this was 0xFFFFFFFFFFFFFFFF." The crash details are shown in the image below: image Call Stack: image ImGui Example crash position: image Memory of Fonts: image

Before this, I was able to run the magnum-triangle and magnum-animated-gif example programs, which are part of the magnum-examples project. Other dependencies were installed using vcpkg, but magnum-integration and imgui were built together as submodules of the magnum-examples cmake project.

WhizZest commented 8 months ago

I have identified the cause of the issue. According to the crash information, it appears that the crash occurs in the imgui.h file, which is located in the vcpkg-installed ImGui library. The version of ImGui installed by vcpkg seems to be outdated, and this may be causing compatibility issues. After running the command "vcpkg remove imgui --recurese," the magnum-imgui program runs successfully.

The incorrect location of imgui.h is shown in the following image:

image

The correct location of imgui.h is shown in the following image:

image

The reason for the program crash is that CMake is finding imgui.h in the wrong location; it should be looking for imgui.h in the magnum-integration project.

Before encountering the issue, I had added the source code of ImGui to the "magnum-integration/src/MagnumExternal" directory. I noticed the following line in the CMakeLists.txt file of ImGuiIntegration: "find_package(ImGui REQUIRED Sources)." Based on my understanding, this command should instruct CMake to look for the imgui.h file in the project source code, not in the vcpkg installation directory.

However, removing vcpkg's ImGui may affect the build of my other CMake projects. Perhaps I need to consider using vcpkg's manifest mode.

mosra commented 3 weeks ago

Sorry for the extremely late reply.

I assume the reason you have added ImGui sources inside MagnumExternal/ in addition to what was installed by vcpkg is the suggestion at https://github.com/mosra/magnum-integration/issues/67#issuecomment-982750056. The underlying linker issue is fixed as of ad50841cfec2d1edd0600d5c87e7be9791754b44, so such a workaround should no longer be needed.

The root cause for the crash you were getting is either due to the two copies are a different version, causing structure layouts to not match, or due to the ImGui context global being duplicated between the two.

In case a similar issue would happen even with the above-referenced fix, and there are no conflicting installations, it's most likely due to the global context pointer being duplicated between the (shared) ImGuiIntegration library and the application. A solution would be to explicitly pass the context pointer between the library and the application-side code. For example:

ImGuiIntegration::Context imgui{...}; // or a member variable
ImGui::SetCurrentContext(imgui.context());

// usual ImGui code after

Or, in the other direction, depending on the application needs:

ImGuiContext* context = ImGui::CreateContext();
ImGuiIntegration::Context imgui{context, ...};

// usual ImGui code after