【问题标题】:Application crashes on std::vector::insert应用程序在 std::vector::insert 上崩溃
【发布时间】: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 相同,insertend 迭代器相同push_back

我四处寻找遇到类似问题的人,我能找到的最接近的是c++ std::vector.insert crashes on debug but works on release,我检查了整个解决方案以查找对memset 的调用,但没有找到与此库相关的任何内容。

感谢任何/所有见解。

【问题讨论】:

  • 我相信您的代码简化了。可能您在插入之前遇到了未定义的行为,但错误仅在此处表现出来。恐怕如果没有上下文,我们将无法解决您的问题
  • 你是如何分配“C 数据结构”的?
  • 鉴于这是第一次插入空向量,我们知道它需要分配内存:水晶球告诉我事先损坏了你的堆。跨度>
  • 尝试编写一段人们可以获取、编译和体验相同行为(即崩溃)的代码。
  • 您不会在某处包含任何可以#define PI 的东西,对吗?假设这是真正的代码,简化。

标签: c++ visual-studio-2010 vector crash


【解决方案1】:

您使用动态链接(例如 /MD)还是静态链接(例如 /MT)?当您在 dll 和应用程序中都有内存操作时(例如,在 dll 中分配内存并在应用程序中删除它),它们都应该使用动态链接进行编译。 要更改链接,请转到属性页 - 代码生成 - 运行库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    相关资源
    最近更新 更多