In my project, I use the constructor of a static object to collect pointers, like a registration method. Very simply, no magic. But during the start I experience a crash, and I can't explain what's happening here. The crash is reproducible on Windows with MSVC or Clang, both using the MSVC headers. Given is the following simple example. Can anyone give me a hint why this could cause issues?
This code seems to work just fine in GCC and Clang on Linux:
https://gcc.godbolt.org/z/vSKdpW
bar.cpp
static int bar = 1;
static Registration abc(&bar);
foo.cpp
static std::vector<void*> registrations;
void add_to_array(void* p)
{
registrations.push_back(p);
}
foo.h
class Registration
{
public:
Registration(void* op)
{
add_to_array(op);
}
};
Executing results in the following crash (_Pnext was 0x8.)
void _Orphan_range(pointer _First, pointer _Last) const { // orphan iterators within specified (inclusive) range
#if _ITERATOR_DEBUG_LEVEL == 2
_Lockit _Lock(_LOCK_DEBUG);
_Iterator_base12** _Pnext = &_Mypair._Myval2._Myproxy->_Myfirstiter;
while (*_Pnext) { <======================= **_Pnext** was 0x8.
Does anyone know why a static vector can't be used to simply collect pointers to objects? foo.cpp is the only file that uses the vector with push_back. The array is not modified anywhere else.