Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifications to avoid Static Initialization Order Fiasco (SOIF) #43

Open
mgrova opened this issue Sep 2, 2024 · 0 comments
Open

Modifications to avoid Static Initialization Order Fiasco (SOIF) #43

mgrova opened this issue Sep 2, 2024 · 0 comments

Comments

@mgrova
Copy link

mgrova commented Sep 2, 2024

Hey there,
first of all thank you for releasing and maintaining this library!

Working with an implementation in which I need quite a lot of states (compared to the examples), around 10, and with a series of static variables that it is necessary to read from the states and modify from outside I have detected that in the library could arise some problem due to SOIF (Static Initialization Order Fiasco).

Do you think it might be a good idea to modify the library to add certain modifications to avoid this problem?
Basically, the idea is to do lazy initialization of the static variables by hiding them in functions with a static variable. It will then be instantiated upon first use.
The major changes would be in the definition of the _state_instance and in the definition of the current_state_pointer.

  • The _state_instance implementation would pass from:
template<typename S>
struct _state_instance
{
  using value_type = S;
  using type = _state_instance<S>;
  static S value;
};

template<typename S>
typename _state_instance<S>::value_type _state_instance<S>::value;

to:

template <typename S>
struct _state_instance
{
    using value_type = S;

    static S& value(){
        static S instance;
        return instance;
    }
};
  • The current_state_pointer implementation would pass from:
static state_ptr_t current_state_ptr;

to:

static state_ptr_t& current_state_ptr()
{
    static state_ptr_t instance(new F);
    return instance;
}

Maybe this doesn't make much sense or is outside the scope of the idea of what the library is written for :-). Let me know pls.

Bests,
Marco.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant