Safe static object initialization
#1
Hi there, I'm new here and will probably be contributing bits here and there whenever I find time.

Now for the other reason behind this thread. I have a static (member) object Tracer::m_NormalTable and need to make sure it isn't used by other static objects before it's initialized. (i.e., calls Tracer::Trace)
Further explanation: https://isocpp.org/wiki/faq/ctors#static-init-order

There are two options:
  1. Initialize on first use
    • Everything works fine. Nobody else needs to care about this.
    • m_NormalTable isn't destructed and 'leaks'


  2. Only add a big fat comment to the Tracer class
    • Has a 50/50 chance of spawning Godzilla, causing cancer or starting a thermonuclear war when someone does use it in another static object
    • Doesn't leak

Which one should be preferred for this project? Option 1 is safer, but causes a small memory leak when the program exits. Option 2 can have catastrophic consequences if someone doesn't know about this.
Reply
Thanks given by:


Messages In This Thread
Safe static object initialization - by Woazboat - 04-30-2015, 03:41 AM
RE: Safe static object initialization - by xoft - 04-30-2015, 06:37 AM



Users browsing this thread: 1 Guest(s)