Cuberite Forum
Safe static object initialization - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Cuberite (https://forum.cuberite.org/forum-4.html)
+--- Forum: Development (https://forum.cuberite.org/forum-13.html)
+--- Thread: Safe static object initialization (/thread-1894.html)



Safe static object initialization - Woazboat - 04-30-2015

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.


RE: Safe static object initialization - worktycho - 04-30-2015

Mostly this project has in the past gone for option 2. Its only recently that we've started moving over to a third option which is newly safer in c++11. Create a free/static getter function with a local static variable. See cLogger::GetInstance() for an example.


RE: Safe static object initialization - NiLSPACE - 04-30-2015

I just want to note that cTracer is deprecated. cLineBlockTracer should be used if possible.


RE: Safe static object initialization - xoft - 04-30-2015

Hello, welcome to the forum Smile

The local static variable is still not safe in MSVC2013, since it doesn't implement "magic statics" yet; it is finally available in MSVC2015. Still, it is the best option out there I guess.

Or maybe the best option would be to get rid of the class altogether. It is poorly documented, no-one really knows what it does, exactly, and it is only used in a few places; as STR already said, we have a newer class for the kind of work this class was expected to perform, with a much improved interface and cleaner documentation.


RE: Safe static object initialization - NiLSPACE - 04-30-2015

I think we planned on removing cTracer since cLineBlockTracer was implemented, so I think we should remove it.