What C++ developers should know about globals (and the linker)
summary
Demonstrates a reproducible segfault when an executable links a static library and a shared library that both provide the same global, showing the constructor runs twice on the same address and the destructor runs twice, causing double free. Uses nm and objdump to inspect symbol tables and init sections, shows linker left-to-right resolution causing the mismatch, and presents fixes: inline variables in C++17, Meyers-style static locals, and a forced static reference trick, plus warnings about SIOF and performance and destructor-timing pitfalls.
AI-written summary · about
chapters (24)
- 0:00Introduction
- 4:58Library types
- 5:23Build setup
- 5:43Compilation commands
- 7:11The problem
- 8:16Debugging initialization
- 8:58Root cause analysis
- 9:55Linker mechanics
- 11:50Symbol tables
- 13:10Linker order
- 14:39Assembly investigation
- 15:37Double construction
- 18:09C++17 solution
- 18:36Pre-C++17 solutions
- 19:53Forced initialization
- 20:50Template tricks
- 22:28Static initialization order
- 23:31Singleton pitfalls
- 24:58Header definition strategy
- 25:52Destructor issues
- 26:13Dependency risks
- 26:59Robust headers
- 27:59Conclusion
- 28:45Q&A
related talks
54mC++ Common Knowledge
30mA Series of Unfortunate Bugs
01h 03mFreestanding C++ - Past, Present, and Future
32mCute C++ Tricks, Part 2 of N: More code you should learn from and never write
16mIntroduction to Procedural Debugging through Binary Libification
40mLessons from two decades of writing bad code
32mVisibility, Reachability, Linkage – The Three Secret Spices of C++ Modules
56mIdentifying Common Code Smells
