On Fri, Dec 30, 2011 at 3:15 PM, Dan Armbrust
<daniel.armbrust.list at gmail.com> wrote:
>
> Yea, it has been known for a while.
>
> I was just proud that I found a fix (hack?) all by myself.
>
> At this point, I'm just trying to understand why it works.
>
> This is the core ubuntu bug (lots of dupes)
> https://bugs.launchpad.net/ubuntu/+source/krename/+bug/849882
>
> The fedora folks figured it out a long time ago (unfortunately, I
> didn't see that thread till I had fixed my own):
> https://bugzilla.redhat.com/show_bug.cgi?format=multiple&id=684908
>
> From the fedora thread:
>
> "this is an issue with the order of static initializations in C++ not
> being defined, and the initializations now running in the wrong
> order."
>
> And, this explains what I didn't know about C++ (and neither did the
> original developer of KRename, apparently)
>
> http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14
>
> Sigh.  I'll stick with Java.
>
Yes, Java defines the order of static initialization, C++ doesn't. The
basic problem with static initialization in C++ is that you can't
guarantee when a static variable will be initialized, especially when
it is used from a different compilation unit (file). If you really
need a static variable, especially one that can be modified, then
here's a pattern that is safe to use:

static A &getA() {
  static A singleton(parameters);
  return singleton;
}

If you put a method like this in your class and then everywhere you
would use the static variable you instead call the function getA, then
you're good. The static variable inside the function is guaranteed to
be evaluated the first time the function is called and therefore you
know the order of initialization. The return value being by reference
(the "&") allows you to assign back to the variable inside the
function:
getA() = some_other_A;



--
Jon Schewe | http://mtu.net/~jpschewe