【发布时间】:2010-09-30 19:15:05
【问题描述】:
我遇到了一个非常奇怪的错误,希望有人能解释一下。我有一个简单的std::vector<V3x>,其中V3x 是一个3d 向量(线性代数类型)。以下代码会引发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 XSI 加载的 .dll 插件中。
有谁知道什么可能导致这样的事情发生?
编辑:解决方案
nobugz,我可以吻你。
由于 VS2008 中的 _HAS_ITERATOR_DEBUGGING,std::vector 的大小在我的 .dll 中发生了变化。搜索将我带到someone with the same problem,并通过在我的项目顶部添加以下内容来修复它:
// fix stack corruption errors caused by VS2008
#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0
【问题讨论】:
-
是 std::vector
顶点;真的是局部变量吗?否则,它很可能尚未创建(因为静态初始化惨败) -
是否 vertices.resize(vertexCount);需要第二个参数吗?
标签: c++ debugging memory parameters vector