std::call_once lazy initialization issue on QNX

Viewed 427

My code is periodically crashing on QNX. It crashes with error

error reading variable: Cannot access memory at address 0x85dd6ac)

while trying to access std::map member variable of 0x85dd6ac object, which is lazy initialized using std::call_once.

Initialization is done with the following pseudo-code:

mutable std::aligned_storage<sizeof(A), alignof(A) >::type m_value;

void init(A *ptr)
{
    new (ptr) A();
}

inline T* data() const
{
    return reinterpret_cast<A*>(&m_value);
}

const A& get() const
{
    std::call_once(m_once_flag, init, data());
    return *data();
}

At some point when object returned by get() is accessed the process is crashed.

On other platforms issue is not reproduced and it's very difficult to debug. From the code I can see that object cannot be uninitialized and it also cannot be deleted at that point.

I suspect there can be issue with std::call_once implementation with thread safety or memory ordering. Does anybody have experience with std::call_once on QNX platform or bugs like that? Any ideas how I can find the issue?

2 Answers
Related