Skip to content

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

@mgrova

Description

@mgrova

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions