Jump to: navigation, search

Dead C++ Society/Material/global objects

Dead C++ Society/Material/global objects


Problems[edit]

  • Constructor call order
  • Destructor call order
  • Dynamic library load order
  • Many things are forbidden to do in DllMain() (windows)
  • Constructors/destructors can throw
  • Static data storage may not be initialized on some platforms
  • Static data storage may be different between different plugins on some platforms

Solutions[edit]

  • Simple solution:
    • Pros/cons: destructor is never run
A *getA()
{
  static A *instance = new A;
  return instance;
}
  • Another simple solution:
    • Pros: No leak - valgrind shows better results
    • Cons: Destructor call order still a problem
A *getA()
{
  static A instance;
  return &instance;
}