digint / digint/tinyfsm

Modifications to avoid Static Initialization Order Fiasco (SOIF)

Open
#43 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
1.2k
Forks
193
PR merge metrics
No merged PRs in 30d

Description

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)](https://en.cppreference.com/w/cpp/language/siof).

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](https://github.com/digint/tinyfsm/blob/01908cab0397fcdadb0a14e9a3187c308e2708ca/include/tinyfsm.hpp#L71) and in the definition of the [current_state_pointer](https://github.com/digint/tinyfsm/blob/01908cab0397fcdadb0a14e9a3187c308e2708ca/include/tinyfsm.hpp#L92).

- The **_state_instance** implementation would pass from:

```
template
struct _state_instance
{
using value_type = S;
using type = _state_instance;
static S value;
};

template
typename _state_instance::value_type _state_instance::value;
```

to:

```
template
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.