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.

one process module A (a shared library) copy A of the library's code copy A's globals current = 0x5a10 allocate = malloc of A module B (a binding) copy B of the same code copy B's globals current = NULL allocate = malloc of B the state object on the heap, at 0x5a10 no pointer to it
The two copies run the same code on different globals. Module B's calls see no current object: the library stops with an assert, or reads through a null pointer.

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:

  1. Check that B's copy is the same version and has the same struct sizes as A's.
  2. Allocator B's allocate and free functions become A's.
  3. Current object B's "current" pointer becomes A's object.
  4. 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.

heap A heap B block B allocates A frees it here

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.

heap A block copy B copy A A's allocate and free

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:

bytes from the start of the context → copy A old fields on ImGuiIO, 3032 B ImGuiStyle the rest … old fields, 32 B copy B old fields off ImGuiIO, 3000 B ImGuiStyle the rest … B reads Style at 3136: in A's object, PlatformIO's end PlatformIO (96 B, amber) sits between IO and Style: at 3072 in A, at 3040 in B. Every later field is 32 bytes off.
Bytes to scale up to the style; the whole context is 10840 bytes in copy A and 10808 in copy B. Copy B's code is right for copy B's headers. It is wrong for an object copy A made.

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:

Turn the old fields on or off in each copy, and pick the field copy B reads. Find the settings that make B read the right bytes, and see what the check says for the others.

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.

A check sees only what it measures

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

module A module B library.dll

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.

module A: library + C wrapper ui_begin(), ui_slider() … module B

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.

module A module B pointer, allocator, version and sizes one object

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_c (C API shared library) RASM: buildUi in beginFrame NewFrame → your callback → Render RASM's copy of Dear ImGui 1.92.6 GImGui → context allocator: RASM's malloc the binding (cimgui, ImGui.NET, …) your UI callback igBegin, igSliderFloat, igEnd its own copy of Dear ImGui 1.92.6 GImGui → the same context allocator: RASM's malloc calls ImGuiContext windows, draw lists, fonts set once, at start: rasm_get_imgui_context …_allocator_functions
Two copies of Dear ImGui, one context. 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

PartThis pageRASM
The objecta state object on the heapThe renderer's ImGuiContext: Renderer::getUiContext, from C rasm_get_imgui_context (null while the UI is off).
Allocatorthe first copy'srasm_get_imgui_allocator_functions gives RASM's copy's ImGui::GetAllocatorFunctions; the binding passes them to its own SetAllocatorFunctions (cimgui: igSetAllocatorFunctions).
Checkversion and sizes, an error instead of an assertrasm_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.
Configurationthe same options on both sidesRASM'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 drawsafter it is set upOnly 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).
3000 BImGuiIO in RASM's Dear ImGui and in cimgui 1.92.63032 B with the old fields
6sizes the check compares, plus the versionIO, style, vec2, vec4, vertex, index
0ImGui functions RASM exportsthe binding brings them

Where it lives in code

WhatWhere
The C callssrc/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 checkUiLayout, getUiLayout, findUiLayoutMismatch in src/renderer/ui.cpp; the start-up check in Renderer::createUi
RASM's Dear ImGuiports/imgui/portfile.cmake
A binding's copycmake/RasmCimgui.cmake (cimgui at its 1.92.6 commit, a shared library of its own, only with RASM_C_IMGUI_EXAMPLE)
Exampleexamples/33_c_render_graph/main.c (shareImGui, drawUi); tests/c_api/ctypes_smoke.py does the same from Python
Keep these true
  • 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.cmake and a new copy of the vcpkg port in ports/imgui.
  • A layout mismatch is an error message, never an assert: createUi keeps the UI off, rasm_check_imgui_layout returns RASM_ERROR_FAILED.
  • RASM's libraries never link cimgui; only the example and the tests do.

Further reading