Re: Strange crashes with std::vector push_back() and emplace_back()
Mathieu Malaterre via Gcc-help <[email protected]> Sat, 18 Jul 2026 14:24:57 +0200
| Newsgroups | gmane.comp.gcc.help |
|---|---|
| Message-ID | <CA+7wUsyK1t+7ruZjYtBRsh0OBwX+KnYAEcysVynvakmJ62moXA@mail.gmail.com> |
Le sam. 18 juil. 2026 =C3=A0 13:18, Undisclosed via Gcc-help <[email protected]> a =C3=A9crit : > > Hi, > some premises first: > -I have no proper debugging skills, plus I have some strict work timeline= s (being into dsp coding) and cannot materially afford to open a "sub-chapt= er" and drift to investigate this problem too much. Plus, I cannot material= ly provide any final code to replicate the crashes, since the code I am pro= viding is part of a music plugin; therefore, I am unable to open any offici= al ticket (in case it is about a bug at all). > -For the reasons above, my message has to be taken as an inquiry to know = if anybody is aware of possible bugs involving relocations in std::vector, = or if anyone has an idea of why I am getting crashes. > > I apologize in advance in case it's me who simply overlooked some deep as= pect of C++ of course... > > -- Illustration of the issue -- > > I am using winlibs msvcrt (gcc 16.4.0 but gcc 14.2.0 had the same issue),= windows 10 x64, i7 8770, codeblocks. > I am using -O3 but I am getting crashes even without any -O option. > The plugin is technically assembled as a DLL and uses static linking for = the C++ libs. > > I have this (simplified) code: > > > class MsgRow // Made as a class so it gets initialized properly on instan= tiation > { > 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_.push_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 p= tr, I get random crashes when push_back() is invoked. > > If I rearrange the code to do the same using emplace_back(): > > auto& new_row =3D messages_.emplace_back(); > // set up new_row fields > > ...I still get random crashes when emplace_back() is called. > > On the contrary, if I do (that's stupid but very illustrative): > > MsgRow new_row; > // set up new_row fields here > messages_.resize(messages_.size()+1); > messages_.back() =3D new_row; > > ...it apparently works without crashing !! > > More clues: > > If in MyPlugin::ctor (not shown) I do: > > messages_.reserve(10); // 10 seems enough > > ...both push_back() and emplace_back() work without apparently crashing. > > Also, if before calling push_back() or emplace_back() I call messages_.ca= pacity() and I intercept its return value for debugging, I see a bogus (ran= dom, often even negative) huge value > > I am very puzzled... > Thanks for any hint. Pretty sure that the bug is outside your usage of std::vector... In any case you can (portably) use _GLIBCXX_DEBUG / _GLIBCXX_DEBUG_PEDANTIC if you think this is related to the usage of std::vector. > > > > --=20 Mathieu