【发布时间】:2013-06-14 18:48:26
【问题描述】:
我的应用程序在我正在处理的应用程序的以下行崩溃:
struct PointIndex
{
int PointNr;
};
struct PointIndex PI = { 0 };
std::vector<PointIndex> MyVec;
MyVec.insert( MyVec.begin(), PI );
所示代码已大大简化(例如,MyVec 实际上是 C 数据结构的成员,直到最近还是 POD,但其中的一些指针已更改为向量以安抚 Coverity),但应用程序 I我的工作量很大,现在,我希望能提供一些关于可能出错的提示。
Visual Studio 2010 将此显示为调用堆栈:
MyAppl.dll!std::_Vector_const_iterator<std::_Vector_val<PointIndex,std::allocator<PointIndex> > >::operator+=(int _Off=-1) Line 157 + 0x15 bytes C++
MyAppl.dll!std::_Vector_iterator<std::_Vector_val<PointIndex,std::allocator<PointIndex> > >::operator+=(int _Off=-1) Line 359 C++
MyAppl.dll!std::_Vector_iterator<std::_Vector_val<PointIndex,std::allocator<PointIndex> > >::operator-=(int _Off=1) Line 371 C++
MyAppl.dll!std::_Vector_iterator<std::_Vector_val<PointIndex,std::allocator<PointIndex> > >::operator-(int _Off=1) Line 376 + 0xc bytes C++
MyAppl.dll!std::vector<PointIndex,std::allocator<PointIndex> >::emplace<PointIndex &>(std::_Vector_const_iterator<std::_Vector_val<PointIndex,std::allocator<PointIndex> > > _Where={PointNr=??? }, PointIndex & _Val={...}) Line 689 + 0x4a bytes C++
MyAppl.dll!std::vector<PointIndex,std::allocator<PointIndex> >::insert<PointIndex &>(std::_Vector_const_iterator<std::_Vector_val<PointIndex,std::allocator<PointIndex> > > _Where={PointNr=??? }, PointIndex & _Val={...}) Line 675 + 0x3b bytes C++
在崩溃时向量是空的,但据我所见,begin 应该与空向量的end 相同,insert 与end 迭代器相同push_back。
我四处寻找遇到类似问题的人,我能找到的最接近的是c++ std::vector.insert crashes on debug but works on release,我检查了整个解决方案以查找对memset 的调用,但没有找到与此库相关的任何内容。
感谢任何/所有见解。
【问题讨论】:
-
我相信您的代码太简化了。可能您在插入之前遇到了未定义的行为,但错误仅在此处表现出来。恐怕如果没有上下文,我们将无法解决您的问题
-
你是如何分配“C 数据结构”的?
-
鉴于这是第一次插入空向量,我们知道它需要分配内存:水晶球告诉我事先损坏了你的堆。跨度>
-
尝试编写一段人们可以获取、编译和体验相同行为(即崩溃)的代码。
-
您不会在某处包含任何可以#define PI 的东西,对吗?假设这是真正的代码,简化。
标签: c++ visual-studio-2010 vector crash