Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;程序挂起在向量析构函数中_C++_C++11_Vector_Destructor - Fatal编程技术网

C++ C++;程序挂起在向量析构函数中

C++ C++;程序挂起在向量析构函数中,c++,c++11,vector,destructor,C++,C++11,Vector,Destructor,我有一个带有字段的结构,该字段添加了一个预处理器#define: 当我向向量添加实例时,当第一次重新分配发生时,程序挂起: vector<MyType> myData; myData.reserve(4); myData.push_back(MyType{0,0}); myData.push_back(MyType{0,1}); myData.push_back(MyType{0,2}); myData.push_back(MyType{0,3}); myData.push_back

我有一个带有字段的结构,该字段添加了一个预处理器#define:

当我向向量添加实例时,当第一次重新分配发生时,程序挂起:

vector<MyType> myData;
myData.reserve(4);
myData.push_back(MyType{0,0});
myData.push_back(MyType{0,1});
myData.push_back(MyType{0,2});
myData.push_back(MyType{0,3});
myData.push_back(MyType{0,4}); // Program hangs
向量myData;
myData.reserve(4);
myData.push_back(MyType{0,0});
myData.push_back(MyType{0,1});
myData.push_back(MyType{0,2});
myData.push_back(MyType{0,3});
myData.push_back(MyType{0,4});//程序挂起
我在前面的行上设置了一个断点,然后逐步完成附加的过程。当旧的向量存储被解除分配时,销毁向量的函数似乎在Microsoft Visual Studio 2015编译器的xmemory0处进入无限循环:

    // TEMPLATE FUNCTION _Destroy_range WITH ALLOC
template<class _Alloc,
class _Ptr = typename _Wrap_alloc<_Alloc>::pointer> inline
void _Destroy_range1(_Ptr _First, _Ptr _Last, _Wrap_alloc<_Alloc>& _Al, false_type)
{   // destroy [_First, _Last), no special optimization
for (; _First != _Last; ++_First) // <----- infinite loop here with _First > _Last
    _Al.destroy(_Unfancy(_First));
}
//模板函数\u销毁\u范围与ALLOC
模板内联
无效-销毁-范围1(\u Ptr\u First,\u Ptr\u Last,\u Wrap\u alloc&\u Al,false\u type)
{//destroy[\u First,\u Last),无特殊优化
对于(;_First!=_Last;++_First)/_Last
_破坏(_unfance(_First));
}
为什么程序挂在那里


(为清晰起见编辑了问题以显示问题)

我在大量调试后发现了问题。 我在结构中有一个define,它向结构中添加了一个额外的成员变量。 当我使用函数构建库并在程序中包含头时,定义仅在其中一个项目中设置


因此,第二个项目删除了(;_First!=_Last;++_First)的
中的数据
循环,但数据结构的成员比用于分配内存的结构多一个。因此循环跳过实际数据的末尾。然后它最后得到
\u Last<\u First
,循环不会终止,因为
\u First
只在循环中增加。

尝试提供一个解决这个问题——而不仅仅是链接到一个包含大量附加代码的文件——将增加您获得有用答案的机会。@allo相关部分是(可能是)问题中包含的问题——仅仅因为您的程序最终在您发布的代码中出现问题,并不意味着这就是真正问题的根源。我建议使用
at()
而不是
[]
访问向量元素,从而验证您是否在某个地方越界。指向代码转储的链接不是要求的。请阅读并返回。使用和调试器
gdb
(特别是监视点)。该错误可能在其他地方出现。请阅读有关向量
单元格在调用
buildCells()
之前是否正常,以及之后是否损坏的详细信息,问题最有可能出现在
buildCells()
内部。
    // TEMPLATE FUNCTION _Destroy_range WITH ALLOC
template<class _Alloc,
class _Ptr = typename _Wrap_alloc<_Alloc>::pointer> inline
void _Destroy_range1(_Ptr _First, _Ptr _Last, _Wrap_alloc<_Alloc>& _Al, false_type)
{   // destroy [_First, _Last), no special optimization
for (; _First != _Last; ++_First) // <----- infinite loop here with _First > _Last
    _Al.destroy(_Unfancy(_First));
}