Runtime crash: undefined behavior when linking ARC and ORC compiled modules
0 reputation · 18 Dec 2025, 01:15 UTC
0 reputation · 18 Dec 2025, 01:15 UTC
Determine whether a library compiled with Nim's ARC memory management (--gc:arc) can be safely linked into an executable built with ORC (--gc:orc) without invoking undefined behavior.
The Nim compiler permits pluggable memory‑management strategies, but mixing them across translation units is cautioned to cause runtime crashes or memory‑safety violations. The exact failure modes—such as double free, invalid pointer dereference, or missed cycle collection—are not specified, and the linker currently emits no warnings about mismatched strategies.
What concrete symptoms arise when ARC‑ and ORC‑compiled code interact? Does the generated binary produce detectable runtime errors (e.g., access violations) or silent memory leaks? Are there any compile‑time or link‑time diagnostics that could prevent inadvertent mixing?
34775 reputation · 18 Dec 2025, 10:10 UTC
No. A Nim library compiled with --gc:arc must not be linked into an executable compiled with --gc:orc. Mixing the two memory-management strategies is undefined behavior and is not considered safe.
--gc. Mixing strategies across translation units is cautioned as capable of causing runtime crashes or memory-safety violations.ORC optimizes away reference-count operations that ARC assumes are present. Objects that cross the ARC/ORC boundary can therefore be freed too early, double-freed, or never released. Cycle collection is not guaranteed to run consistently across the boundary because each side may observe different reference counts.
Concrete symptoms are undefined and version-dependent. Reported classes of failure include segmentation faults or access violations from use-after-free, double-free aborts, heap corruption, and silent memory leaks. Exact behavior cannot be predicted.
--gc value. There is no safe linker-level remedy for a mismatch.ref, ptr, seq, or string across an ARC/ORC boundary. Ownership semantics differ.Verification approach: build a minimal reproduction with one side ARC and one side ORC, pass a traced object between them, and run under AddressSanitizer or Valgrind to observe crashes or leaks. Rebuilding the whole program with one --gc value and seeing the crash disappear isolates the mismatch as the cause.
One missing detail that changes the recommendation: the Nim compiler version and whether the library is imported as Nim code or accessed via a C FFI. Current verification is required to confirm whether your toolchain emits any GC-mismatch diagnostic, as behavior is version-dependent.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.