View Issue Details
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0001280||OpenMPT||[All Projects] libopenmpt||public||2019-11-06 16:02||2019-11-10 17:47|
|Product Version||OpenMPT 1.29.00.* (current testing)|
|Target Version||OpenMPT 1.28.09.00 / libopenmpt 0.4.11 (upcoming stable)||Fixed in Version|
|Summary||0001280: ban function-static data|
We must ban function-local static data.
static data inside of a function must use a lock to be initialized thread-safely by the compiler. This is required in C++11 and implemented even for C++03 by gcc and clang. The compiler is optionally allowed to use constant-initialization instead, but is by no means and in no circumstances required to do so (not even when the data is declared const and not even if the initializing expression is a constant expression). Current MSVC doesn't use constant-initialization in debug builds and thus introduces a lock, and older MSVC just silently introduces a race condition because it does not implement thread-safe initialization.
static const data inside a function is a bug, in both, C++ pre 11, and post 11.
This affects all active branches:
There are 3 alternatives:
Note: function-local static data is no bug in C, because C requires initializers to be constant expressions.
|Tags||No tags attached.|
|Has the bug occurred in previous versions?|
|Tested code revision (in case you know it)|
For static LUTs in module loaders, typically for pattern command conversion, I prefer them to be close to their usage site, so constexpr initialization seems to be the best candidate there. Otherwise putting them at the top of the file with constexpr would be fine too. Apart from a few static const variables here and there (which could also be trivially turned into constexpr), I guess those would be the biggest offenders in soundlib/?