My program crashes on this line but only in debug build, it works fine in release build.
m_lstIds.insert(m_lstIds.begin() + indexInsert, ID);
'm_lstIds' is a std::vector of int, 'ID' is an int. When the program crashed, m_lstIds had 3 items (1, 2, 3). indexInsert was '0' and ID was '0'.
The error message says:
Expression: vector iterator + offset out of range
I am running visual studio 2010; I am guessing it has something to do with bad project settings which conflicted with STL optimization.
Edit:
When I said: "works on release" I meant if I do std::cout<<m_lstIds[i] for i = 0..3, I will actually get 0,1,2,3 printed out. In debug build it just crashes when I try to insert.
Edit2: I found the answer! Thanks everyone for the help.
Here is the shortest repro. The problem is the memset function I call at the constructor. Because the constructor of m_lstItem was called before the memset, it will erase whatever data in the vector that allowed insert to work properly.
What's really interesting is how this worked in release but not in debug. Would be great if someone can explain that part.
struct SimpleList
{
SimpleList()
{
memset(this, 0, sizeof(SimpleList));
m_lstItem.push_back(0);
m_lstItem.push_back(1);
m_lstItem.push_back(2);
}
void Crash()
{
m_lstItem.insert(m_lstItem.begin() + 0, 3);
}
std::vector<int>m_lstItem;
};
int main(int argc, char** argv[])
{
SimpleList sl;
sl.Crash();
return 0;
}
memset(this, 0, sizeof(SimpleList));DON'T do this unless your class is POD !!std::vectorMUST be initialized with its own constructor. - fefe