-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
SetAllocatorFunctions() accross DLL #3836
Comments
The core issue is you essentially have two copies of Dear ImGui in your program: One in the executable and one in the library. This means you have two copies of In my opinion, the proper solution to this is either:
Otherwise I think you've gone into weird setup territory and it's not a huge ask for you to call
Maybe the wording could be improved, but the hot reloading comments you're referring to does:
(Extra emphasis on "or") The comments focus on hot reloading because that's just the most common reason people have multiple copies of ImGui within the same app. |
Thanks for the prompt answer. Let me reiterate, as maybe my intention was lost among that wall of text I left:
Hope I was a bit clearer this time. |
Hello, I'll add those comments then.
If you don't install allocators then things should just work without any calls? |
Hi! Otherwise, when the DLL calls ImGui functions causing allocations, it will use it's own heap, then when the EXE calls Now the only way I found to make sure the DLL uses the same allocators as the EXE (please correct me if there's another way), is to get the addresses of |
See 6f4b9c6 |
Seems great! Thanks for the quick resolution! |
…her functions. + Made the ImGuiMemAllocFunc / ImGuiMemFreeFunc consistent with our other typedefs (#3836)
commit ee643b2 Author: ocornut <omarcornut@gmail.com> Date: Thu Mar 4 19:59:59 2021 +0100 IsItemHovered(): fixed return value false positive when used after EndChild(), EndGroup() or widgets using either... (ocornut#3851, ocornut#1370) ...when the hovered location is located within a child window, e.g. InputTextMultiline(). This is intended to have no side effects, but brace yourself for the possible comeback.. This essentially makes IsItemHovered() not accept hover from child windows, but EndChild/EndGroup are forwarded. More or less should fix/revert c76f014 which was a revert of 344d48b commit b53b8f5 Author: Rokas Kupstys <rokups@zoho.com> Date: Thu Mar 4 16:27:43 2021 +0200 Demo: Use correct string formats on non-windows platforms. (amended) commit 3e6dfd3 Author: ocornut <omarcornut@gmail.com> Date: Thu Mar 4 13:37:14 2021 +0100 ImDrawList: AddImageRounded() compare texid from cmdheader as with other functions. + Made the ImGuiMemAllocFunc / ImGuiMemFreeFunc consistent with our other typedefs (ocornut#3836) commit 8dd692c Author: ocornut <omarcornut@gmail.com> Date: Thu Mar 4 11:03:40 2021 +0100 Android: Amend backend and examples with minor consistency tweaks. (ocornut#3446) commit fb85c03 Author: duddel <duddel@users.noreply.github.com> Date: Thu Mar 4 10:35:44 2021 +0100 Add Android backend and example (ocornut#3446) commit d8c88bd Author: ocornut <omarcornut@gmail.com> Date: Thu Mar 4 09:52:00 2021 +0100 Tables: Fixed unaligned accesses when using TableSetBgColor(ImGuiTableBgTarget_CellBg). (ocornut#3872) ImSpanAllocator: Support for alignment. commit 1ddaff8 Author: ocornut <omarcornut@gmail.com> Date: Wed Mar 3 18:45:52 2021 +0100 Demo: Tweak inputs display.
Version/Branch of Dear ImGui:
Version: 1.77
Branch: master
Back-end/Renderer/Compiler/OS
Back-ends: custom
Compiler: MSVC 19.16.27035
Operating System: Windows 10
My Issue/Question:
In my setup I have an exe that loads a dll containing game code. The game code contains most of the ImGui code, however the per-frame functions (NewFrame, Render..) happen in main(), which is in the exe of course.
I've been using ImGui like this for some years now, however after a recent upgrade I started getting a crash. The crash was always in a debug CRT heap, with the message
I decided to track it down, because it was very reproducible, and it turns out it's caused by the new(ish) GcCompactTransientWindowBuffers (called from NewFrame(), if I'm not mistaken). I got a clue from a stack overflow answer that this can usually happen due to allocations across a DLL boundary, which is exactly what's happening here.
I am using the default allocators provided (i.e: malloc & free), meaning both GImAllocatorAllocFunc & GImAllocatorFreeFunc in imgui.cpp are different for the exe & dll and hence use separate heaps. My assumption is this has not been a problem so far because ImGui wasnt freeing much until that GC process was introduced? (and ofc I'm not even calling DestroyContext during shutdown).
This problem is easily fixed if I make a call during the setup of my DLL to SetAllocatorFunctions() next to my current SetContext() call, passing in the global allocators used by the exe (I had to hack an accessor to those, since the variables are static in imgui.cpp).
I understand this is not a bug in the library per se.. however I feel the situation for this kind of use case could be improved a bit. There is mention of the need to call SetAllocatorFunctions when hot-reloading, however please note this is a problem caused just by the bridge EXE - DLL itself, and it happens even if the dll is just loaded once on startup. I'm kind of surprised this hasn't been noted before, to be honest.
I feel the comments should clarify the need to do something similar to what I did whenever ImGui code is used across dynamically linked libraries, and also provide a GetAllocatorFunctions() or similar. I don't know.. just wanted to raise this as I thought maybe this wasn't too visible.
(Sorry I didn't provide a code example, but this requires quite a specific setup and not just code).
The text was updated successfully, but these errors were encountered: