我碰到了一个很奇怪的虫子,我希望有人能解释。我有一个简单的std::vector<V3x>,其中V3x是一个三维向量(线性代数类)。以下代码将引发std::length_error异常:
std::vector<V3x> vertices;
int vertexCount = computeVertexCount();
vertices.resize(vertexCount); // throws std::length_error我已经验证了computeVertexCount()返回一个35的值,这个值远低于vector::max_size(),所以它不可能要求太多的内存。
我将异常追溯到std::vector的定义中,包括以下两个函数。
void resize(size_type _Newsize, _Ty _Val)
{ // determine new length, padding with _Val elements as needed
if (size() < _Newsize)
// NOTE: here, _Newsize - size() = 35
_Insert_n(end(), _Newsize - size(), _Val);
else if (_Newsize < size())
erase(begin() + _Newsize, end());
}
void _Insert_n(const_iterator _Where,
size_type _Count, const _Ty& _Val)
{ // insert _Count * _Val at _Where
// NOTE: here, _Count = 3435973836
...
}因此,当_Count参数在resize()和_Insert_n()之间传递时,值从35更改为3435973836。我假设内存已经被破坏了,但我不知道这是怎么回事。
如果这是问题的一部分,为了获得更多的上下文,这段代码位于我从Softimage加载的一个.dll插件中。
有人知道什么会导致这种事发生吗?
编辑:解决方案
nobugz我可以吻你。
由于.dll中的_HAS_ITERATOR_DEBUGGING,std::载体的大小在改变。搜索使我找到了有同样问题的人,并且通过在我的项目顶部添加以下内容来修复它:
// fix stack corruption errors caused by VS2008
#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0发布于 2009-01-11 19:46:29
https://stackoverflow.com/questions/433274
复制相似问题