Strange crashes with std::vector push_back() and emplace_back()
Undisclosed via Gcc-help <[email protected]> Fri, 17 Jul 2026 13:28:29 +0000
| Newsgroups | gmane.comp.gcc.help |
|---|---|
| Message-ID | <[email protected]> |
Hi,
some premises first:
-I have no proper debugging skills, plus I have some strict work timelines=
(being into dsp coding) and cannot materially afford to open a "sub-chapte=
r" and drift to investigate this problem too much=2E Plus, I cannot materia=
lly provide any final code to replicate the crashes, since the code I am pr=
oviding is part of a music plugin; therefore, I am unable to open any offic=
ial ticket (in case it is about a bug at all)=2E
-For the reasons above, my message has to be taken as an inquiry to know i=
f anybody is aware of possible bugs involving relocations in std::vector, o=
r if anyone has an idea of why I am getting crashes=2E
I apologize in advance in case it's me who simply overlooked some deep asp=
ect of C++ of course=2E=2E=2E
-- Illustration of the issue --
I am using winlibs msvcrt (gcc 16=2E4=2E0 but gcc 14=2E2=2E0 had the same =
issue), windows 10 x64, i7 8770, codeblocks=2E
I am using -O3 but I am getting crashes even without any -O option=2E
The plugin is technically assembled as a DLL and uses static linking for t=
he C++ libs=2E
I have this (simplified) code:
class MsgRow // Made as a class so it gets initialized properly on instant=
iation
{
public:
bool clear =3D false;
bool dsp =3D false;
wchar_t chars[50] =3D {};
};
class MyPlugin
{
public:
void enqueueMessage(void *event)
{
if(!event) return;
MsgRow new_row;
// filling new_row fields here
messages_=2Epush_back(new_row); // this crashes
}
// more methods
private:
std::vector<MsgRow> messages_;
// more stuff
}
What happens is that when enqueueMessage() is called with a valid event pt=
r, I get random crashes when push_back() is invoked=2E
If I rearrange the code to do the same using emplace_back():
auto& new_row =3D messages_=2Eemplace_back();
// set up new_row fields
=2E=2E=2EI still get random crashes when emplace_back() is called=2E
On the contrary, if I do (that's stupid but very illustrative):
MsgRow new_row;
// set up new_row fields here
messages_=2Eresize(messages_=2Esize()+1);
messages_=2Eback() =3D new_row;
=2E=2E=2Eit apparently works without crashing !!
More clues:
If in MyPlugin::ctor (not shown) I do:
messages_=2Ereserve(10); // 10 seems enough
=2E=2E=2Eboth push_back() and emplace_back() work without apparently crash=
ing=2E
Also, if before calling push_back() or emplace_back() I call messages_=2Ec=
apacity() and I intercept its return value for debugging, I see a bogus (ra=
ndom, often even negative) huge value
I am very puzzled=2E=2E=2E
Thanks for any hint=2E