Re: Strange crashes with std::vector push_back() and emplace_back()
Jonathan Wakely via Gcc-help <[email protected]> Sat, 18 Jul 2026 15:02:48 +0100
| Newsgroups | gmane.comp.gcc.help |
|---|---|
| Message-ID | <CAH6eHdTwtLMEnpvfQ81Se4PBT9LJsD=V1L4oxfU0h-oM4gJe1Q@mail.gmail.com> |
On Sat, 18 Jul 2026, 12:18 Undisclosed via Gcc-help, <[email protected]> wrote: > 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-chapter" and drift to investigate this problem too much. Plus, I > cannot materially provide any final code to replicate the crashes, since > the code I am providing is part of a music plugin; therefore, I am unable > to open any official 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 am confident this is not a bug in std::vector. > I apologize in advance in case it's me who simply overlooked some deep > aspect 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 don't understand the Windows linkage model, but are all the allocations and deallocations done in the same DLL? Each DLL has its own heap so if you allocate memory by calling enqueueMessage in dll A and then deallocate that memory by calling it again (so that it resizes the vector) from a different dll, B, you will attempt to return memory from heap A to heap B, which can corrupt memory, and/or crash. > I have this (simplified) code: > > > class MsgRow // Made as a class so it gets initialized properly on > instantiation > { > public: > > bool clear = false; > bool dsp = false; > wchar_t chars[50] = {}; > }; > > > > 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 > ptr, I get random crashes when push_back() is invoked. > > If I rearrange the code to do the same using emplace_back(): > > auto& new_row = 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() = 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_.capacity() and I intercept its return value for debugging, I see > a bogus (random, often even negative) huge value > > I am very puzzled... > Thanks for any hint. > > > > > >