Foundations
Two copies of one library
A static library is copied into every module that links it: an executable, and each shared library (DLL). Two modules of one process that link the same library hold two copies of its code and of its globals. This page shows what that breaks, and the three things the copies must share to work on one object: its pointer, the allocator, and the struct layout. The last part shows how a language binding draws Dear ImGui windows into RASM's frame this way.
In short
- Each copy has its own globals: a "current object" pointer, an allocator, caches. Setting one copy's global does not set the other's.
- The object itself lives on the heap, so both copies can use it once the second copy is given its pointer.
- Memory one copy allocates, the other may free. Both must use the same allocator, or a free goes to the wrong heap.
- Both copies must agree on every struct's layout: the same version and the same compile-time options. A different option moves fields, and the second copy reads the wrong bytes.
- A version and size check at the start turns a layout mismatch into a clear error instead of memory corruption. It catches only what it measures.
1One library, two copies
Look at one process. A shared library links a UI library statically and makes the UI's state object. A second module, a language binding, links the same UI library statically too.
Nothing here is wrong in the link step. Each module is complete on its own, and the linker gives each one the library's code it needs. The problem shows only when the two modules work on one object.
2What the copies must share
The object can be shared, because it is plain memory. What cannot be shared are the globals: each copy reads its own. So module A hands over what copy B needs, and copy B sets its own globals from it, before its first call:
- Check that B's copy is the same version and has the same struct sizes as A's.
- Allocator B's allocate and free functions become A's.
- Current object B's "current" pointer becomes A's object.
- Call B's code now works on the shared object, and so does A's.
The order matters. The check comes first, because a copy with another layout must not touch the object at all. The allocator comes before any call that can allocate.
3One allocator for both
The object grows while both copies use it: B's call adds a window, A's call later frees it. Each copy allocates with its own global allocator, and on Windows each module can have its own C runtime, with its own heap.
Two allocators: a free goes to the wrong heap
The heap gets a block it never gave out. The program crashes later, somewhere else, or corrupts memory without a sign.
One allocator: every block comes back home
B takes A's functions (and their user data) before its first call. Which copy allocates and which frees no longer matters.
4The same layout, byte for byte
Copy B reads the object with its own idea of where each field is. That idea comes from the headers and the options B was compiled with. A compile-time option that adds a field, or changes a type, moves every field after it.
A real case, Dear ImGui 1.92.6 on a 64-bit machine. Its option IMGUI_DISABLE_OBSOLETE_FUNCTIONS removes four old fields from the end of ImGuiIO: a float and three pointers, 32 bytes with padding. The state object (ImGuiContext) holds ImGuiIO near its start, so everything after it moves by 32 bytes:
Such a mismatch does not crash at once. Copy B writes a style color into the platform callbacks, or reads a window list from the middle of another field. The crash, if one comes, comes later and far away. Try it:
5Check before the first call
The fix is a check at the start, before copy B touches the object. Copy B passes what it was compiled with, and the library compares it with its own numbers:
versionB = versionA
sizeof(T)B = sizeof(T)A, for each T in the list
- version
- the version string from the headers, compiled into each copy
- T
- the structs whose size shows an option: the IO and style structs, the vector types, the vertex and index types
Dear ImGui's own IMGUI_CHECKVERSION is this check. It passes the version and six sizes to DebugCheckVersionAndDataLayout, which asserts on the first difference. Across a library boundary an assert is the wrong answer: the caller may be another language, and a release build has no asserts. So a check for the other copy returns an error that names the first difference.
Six sizes do not prove that every struct matches. An option that changes a struct not in the list, with no effect on the sizes in it, passes the check and still breaks. The rule stays: the same version, built with the same configuration file and the same defines. The check catches the common mistakes, such as the old-fields option or a 32-bit index type.
6Other ways to avoid two copies
One shared copy
Build the library as a shared library and link both modules to it. One copy, one set of globals.
Every user must ship the same build of it; a C++ interface across it has no stable ABI.
A wrapper in the first module
Module A exports C functions for the whole library, and B calls those. One copy again.
Hundreds of functions to keep stable, and the binding's own wrapper goes unused.
Hand over the state (this page)
Two copies stay, and share the object. Each module keeps the library it was built with.
Both copies must be the same version and configuration; the check guards it.
7Pitfalls
- The second module's first call asserts "no current context"
- Its copy's current-object global is still null. Set it from the pointer the first module gives, in that copy.
- A crash in free, in a frame long after the UI call
- The copies use different allocators, and one frees what the other allocated. Give the second copy the first one's allocate and free functions before its first call.
- Strange values in fields that were never written
- The copies were built with different options or versions, so a field sits at another offset in each. Build both with the same configuration, and run the check first.
- It works in Debug and breaks in Release
- The library's own check asserts, and the assert is compiled out in Release. Use a check that returns an error.
- It worked until one dependency update
- One copy moved to a new version, the other did not. Pin both to one version, and update them together.
- A crash after the first module unloads or destroys the object
- The second copy still points to it. Clear its current pointer when the owner destroys the object.
8In RASM
RASM links Dear ImGui statically: into an application with rasm::rasm, and into the C API's shared libraries rasm_c and rasm_c_renderer. A language binding brings its own Dear ImGui: cimgui (C, and the base of ImGui.NET and imgui-rs), or the binding's own build. So a C program or a binding is exactly the two-copies case, and RASM ships no ImGui wrapper of its own. The binding's copy draws into RASM's context through C API 1.19 (Dear ImGui from another language):
rasm_check_imgui_layout runs first; the UI pass then draws what both copies built (the UI in RASM).Where RASM follows the page, and where it differs
| Part | This page | RASM |
|---|---|---|
| The object | a state object on the heap | The renderer's ImGuiContext: Renderer::getUiContext, from C rasm_get_imgui_context (null while the UI is off). |
| Allocator | the first copy's | rasm_get_imgui_allocator_functions gives RASM's copy's ImGui::GetAllocatorFunctions; the binding passes them to its own SetAllocatorFunctions (cimgui: igSetAllocatorFunctions). |
| Check | version and sizes, an error instead of an assert | rasm_check_imgui_layout(version, io, style, vec2, vec4, drawVert, drawIdx): the six sizes of IMGUI_CHECKVERSION, compared by findUiLayoutMismatch; RASM_ERROR_FAILED names the first difference. rasm_get_imgui_version gives IMGUI_VERSION and IMGUI_VERSION_NUM. |
| Configuration | the same options on both sides | RASM's Dear ImGui is vcpkg's port with one change (ports/imgui): IMGUI_DISABLE_OBSOLETE_FUNCTIONS in its imconfig.h. cimgui's headers are generated with it, so stock cimgui, ImGui.NET and imgui-rs match. 16-bit ImDrawIdx, 16-bit ImWchar. |
| Static C++ users | — | An application of the installed rasm::rasm links the Dear ImGui of its own vcpkg, which must be built the same way. createUi measures the linked library against RASM's headers: its version, where it puts the context's IO and PlatformIO (the size of ImGuiIO), and how far its own code moves a draw list's write pointers for one rectangle (the sizes of ImDrawVert and ImDrawIdx). On a difference it logs both sizes and keeps the UI off, with no assert. RASM makes its context current nowhere yet (D59). |
| When the copy draws | after it is set up | Only inside the UI callback (rasm_set_ui_callback), between RASM's NewFrame and Render, on the thread that renders, with the rules of a callback (Callbacks). |
Where it lives in code
| What | Where |
|---|---|
| The C calls | src/c/cUi.cpp: rasm_set_ui_callback, rasm_get_ui_texture, rasm_get_imgui_context, rasm_get_imgui_allocator_functions, rasm_get_imgui_version, rasm_check_imgui_layout |
| The check | UiLayout, getUiLayout, findUiLayoutMismatch in src/renderer/ui.cpp; the start-up check in Renderer::createUi |
| RASM's Dear ImGui | ports/imgui/portfile.cmake |
| A binding's copy | cmake/RasmCimgui.cmake (cimgui at its 1.92.6 commit, a shared library of its own, only with RASM_C_IMGUI_EXAMPLE) |
| Example | examples/33_c_render_graph/main.c (shareImGui, drawUi); tests/c_api/ctypes_smoke.py does the same from Python |
- RASM's Dear ImGui and cimgui's headers agree: the same version, and IMGUI_DISABLE_OBSOLETE_FUNCTIONS on both. A new Dear ImGui means a new cimgui commit in
cmake/RasmCimgui.cmakeand a new copy of the vcpkg port inports/imgui. - A layout mismatch is an error message, never an assert:
createUikeeps the UI off,rasm_check_imgui_layoutreturnsRASM_ERROR_FAILED. - RASM's libraries never link cimgui; only the example and the tests do.
Further reading
- Omar Cornut, Dear ImGui, docs/FAQ.md ("How can I use Dear ImGui across DLL boundaries?") and the comments at SetAllocatorFunctions in imgui.h.
- cimgui, its README.md on how its headers are generated.
- A stable C ABI, Immediate-mode UI, Struct layout rules.