Nim GC Mode Switching Affects Linked Object Files and Manual Memory Portability
0 reputation · 07 Jan 2023, 12:25 UTC
0 reputation · 07 Jan 2023, 12:25 UTC
Investigate whether switching the Nim garbage collector mode via the `gc` pragma influences the runtime behavior of pre‑existing object files and whether manual memory management with `gc none` remains portable across different target platforms without recompiling all linked modules.
Does the new collector mode take effect for all linked object files when only the source file is recompiled, or is a full rebuild mandatory? Can manual memory management with `gc none` be safely used on platforms with differing stack‑scanning implementations without additional recompilation?
35275 reputation · 07 Jan 2023, 23:04 UTC
No. A `gc` pragma or `--gc` flag is compiler configuration, not a patch for object files already on disk. Recompiling one source file applies the new collector mode to that translation unit only; sibling object files retain the allocation calls, write barriers, finalizer registrations, and stack-scanning assumptions they were compiled with. For a consistent whole-program collector, recompile every Nim module that allocates, traces, finalizes, or references GC-managed objects, then relink. `gc none` is likewise not portable across platforms: object files are tied to a target OS, architecture, and ABI, and differing stack-scanning implementations require a fresh target-specific build and test pass.
| Item | Status |
|---|---|
| One recompiled module changes only its own object file | Confirmed for standard Nim code generation |
| Mixed GC-mode object files can disagree about heap ownership | Confirmed |
| `gc none` disables tracing but does not create cross-platform binaries | Confirmed |
| Exact pragma spelling, `--gc` values, and runtime symbol names | Varies by Nim release; verify against your compiler |
| Whether `gc none` fully disables stack scanning | Varies by Nim release; verify against your compiler |
Assumptions: standard static linking of the Nim runtime and no link-time optimization that merges GC models across object files.
Inspect object files for collector references before and after a selective rebuild:
nm module.o | grep -i gcUnchanged GC references in the old object confirm the pragma did not rewrite it. Rebuild all modules with one identical `--gc` value and compare behavior; if crashes disappear, a full rebuild was required.
Provide your exact Nim version and whether you use a procedure-scoped `{.gc: ...}` pragma or the global `--gc` flag. That detail determines whether the rebuild scope covers annotated procedures only or whole modules, so it changes the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.