C ++参数的值在std :: vector中的堆栈帧之间的变化 [英] C++ Parameter's Value Changes Between Stack Frames in std::vector

查看:160
本文介绍了C ++参数的值在std :: vector中的堆栈帧之间的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个非常奇怪的错误,我希望有人可以解释。我有一个简单的 std :: vector< V3x> ,其中 V3x 是一个3d向量(线性代数类。 )以下代码导致抛出 std :: length_error 异常:

  std :: vector< V3x>顶点; 
int vertexCount = computeVertexCount();
vertices.resize(vertexCount); // throws std :: length_error

我已经验证了 computeVertexCount() 返回值 35 ,远低于 vector :: max_size()所以没有办法要求太多的记忆。



我将异常追溯到 std :: vector ,到以下两个函数。

  void resize(size_type _Newsize,_Ty _Val)
{//确定新的长度,使用_Val元素填充
if(size()< _Newsize)
//注意:_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)
{//插入_Count * _Val在_Where
//注意:这里_Count = 3435973836
...
}

所以当 _Count 参数在 resize() _Insert_n(),值从35变为3435973836.我假设记忆已经不知何故已经破坏了,但我不知道该怎么做。



有一点更多的上下文,以防万一这是问题的一部分,这段代码放在我从Softimage XSI加载的.dll插件。



有谁知道什么可能会导致类似这会发生吗?



编辑:解决方案



nobugz,我可以亲你



由于VS2008中的 _HAS_ITERATOR_DEBUGGING ,std :: vector的大小正在更改.dll内。搜索引导我进入有同样问题的人,通过在项目顶部添加以下内容来修复:

  / /修复由VS2008导致的堆栈损坏错误
#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0


解决方案

值3435973836是重要的。在十六进制,这是0xcccccccc。这是通过堆栈帧初始化代码在调试模式下分配给局部变量的值。当你在调试时看到它,你会说啊,变量未初始化。也许这让你更加接近解决这个问题。



你提到DLL。这也是有用的。迭代器调试可能会让您陷入困境,您不能将其关闭的代码与不具有代码的代码混合在一起。由于DLL可能是没有它的编译,请尝试#define _HAS_ITERATOR_DEBUGGING 0。


I've run into a really strange bug, that I'm hoping someone can explain. I have a simple std::vector<V3x>, where V3x is a 3d vector (the linear algebra kind.) The following code causes a std::length_error exception to be thrown:

std::vector<V3x> vertices;
int vertexCount = computeVertexCount();
vertices.resize(vertexCount); // throws std::length_error

I've verified that computeVertexCount() returns a value of 35, which is far far below vector::max_size() so there's no way it's asking for too much memory.

I traced the exception down into the definition of std::vector, to the following two functions.

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
        ...
    }

So when the _Count parameter is passed between resize() and _Insert_n(), the value changes from 35 to 3435973836. I'm assuming the memory has somehow become corrupted, but I have no idea how that could be.

For a little more context in case it's part of the problem, this code sits in a .dll plugin that I'm loading from Softimage XSI.

Does anyone know what might cause something like this to happen?

EDIT: SOLUTION

nobugz, I could kiss you.

The size of std::vector was changing inside my .dll, because of _HAS_ITERATOR_DEBUGGING in VS2008. The search led me to someone with the same problem, and it was fixed by adding the following at the top of my project:

// fix stack corruption errors caused by VS2008
#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0

解决方案

The value 3435973836 is significant. In hex, that's 0xcccccccc. That's the value assigned to local variables in Debug mode by the stack frame initialization code. When you see it back while debugging, you'd say "ah, variable not initialized". Maybe that gets you a bit closer to solving this.

You mention DLL. That's relevant too. Iterator debugging might get you into trouble, you cannot mix code that has it turned off with code that doesn't. Since the DLL is probably compiled without it, try #define _HAS_ITERATOR_DEBUGGING 0.

这篇关于C ++参数的值在std :: vector中的堆栈帧之间的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆