init: workaround a double free in cookie allocation

This commit is contained in:
Danny Robson 2021-04-09 13:28:49 +10:00
parent f5a1105d48
commit d5139c2cc4

View File

@ -31,28 +31,25 @@ namespace {
cruft::parse::enumeration::cookie log;
};
std::unique_ptr<state> s_state;
std::atomic<int> s_count;
static std::unique_ptr<state> s_state;
}
//-----------------------------------------------------------------------------
// Increase the reference count and initialising the requisite functionality if
// this is the first instance of the class.
// NOTE: We were using reference counting to cleanup s_state at cookie
// destruction time, but for some reason this results in a double-free under
// gcc-usan. It's easier to just allow it to live past the useful period and
// perform cleanup atexit.
static void up (void)
{
if (int old = s_count++; !old)
if (!s_state)
s_state = std::make_unique<state> ();
}
//-----------------------------------------------------------------------------
// Decrease the reference count and release any resources if this was the last
// instance of the class.
static void down (void)
{
if (int old = --s_count; !old)
s_state.reset ();
}