对于来自UNIT测试的构造函数中的某些reasone字段初始化,会破坏内存。
我有以下课程
//.h
class Entity
{
public:
...
Entity();
private:
unsigned int _nextOperatorId;
unsigned int _operators[30][4]; //from consts
...
}
//.cpp
Entity::Entity() : _operators(), _nextOperatorId(1)
{
/* If i run this from unit test i see:
_operators [0] 0x0569bb38 {3452816845, 3452816845, 3452816845, 3452816845, 1}
_operators [1] 0x0569bb4c {3452816845, 0, 0, 0, 0}
_operators [2] 0x0569bb4c {0, 0, 0, 0, 0}
... (all other rows are zeroes).
If i delete _nextOperatorId(1) initialiazation, or if i run constructor from console app, here all as expected - all rows in operators array are zeroes * /
}
我在VS单元测试类初始化程序中运行它,如下所示:
private
Entity* entity;
public:
TEST_METHOD_INITIALIZE(ClassInitialize)
{
entity = new Entity();
}
那么为什么在添加_nextOperatorId(1)之后内存会被破坏?一切看起来都那么简单..
答案 0 :(得分:2)
VC ++编译器中存在已知错误;除非在您的版本中修复,否则您不能依赖VC ++来按照C ++标准的要求对类成员进行初始化。
http://connect.microsoft.com/VisualStudio/feedback/details/564268/c-value-initialization