I’m using visual studio 2012 on windows 8, and the 2012 november CTP.
When I run the release version of my c++ application with optimization enabled (/o2) it crashes.
It happens inside a std::vector (not the first time I use vectors though).
Stepping inside the code I found out that the second time I do a push_back, here:
1197 if (this->_Mylast == this->_Myend)
1198 _Reserve(1);
if I follow _Reserve, the value of count from 1 becomes 17656451, resulting in an error later when he it calls reallocate (I have exceptions disabled).
When running in debug mode everything is fine. Also release mode with optimizations disabled is running fine.
Could it be because the compiler is a test release? I’m using some of the new features so I can’t switch back and try.
edit:
Here is part of the code.
I have two structures:
struct Subtile
{
int sequenceId;
AnimationSequenceState sequenceState;
XMFLOAT2 position;
};
struct Tile
{
//int type;
bool visible;
bool walkable;
int x, y;
int height;
XMFLOAT2 position;
float depth;
XMVECTOR color;
std::vector<Subtile> subtiles;
};
And this function gets called from inside a loop when parsing a 2d map of a game from a lua file:
HRESULT Map::parseTile(LuaState& l, int ix, int iy, XMFLOAT2 pos)
{
Tile tile;
tile.visible = l.getBoolField(-1, parsing::MapTileVisible, true);
tile.walkable = l.getBoolField(-1, parsing::MapTileWalkable, true);
tile.height = l.getIntField(-1, parsing::MapTileHeight, 0);
bool sync = l.getBoolField(-1, parsing::MapTileSync, true);
tile.x = ix;
tile.y = iy;
tile.position.x = pos.x;
tile.position.y = pos.y;
tile.depth = -static_cast<float>(ix + iy);
tile.color = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f);
l.pushString(parsing::MapTileSubtiles);
l.getTable();
{
for(l.pushNil(); l.next(); l.pop())
{
Subtile subtile;
subtile.sequenceId = l.toInt();
_sequences[subtile.sequenceId].update(subtile.sequenceState);
if(!sync)
{
subtile.sequenceState.elapsed = rand() % 1000; //一秒、適当に
}
tile.subtiles.push_back(subtile);
}
}
l.pop();
_tiles.push_back(tile); //second time crashes here
return S_OK;
}
edit2:
Here is a more complete part of the code -> LINK
I tried to localize the problem and here is what I found.
Removing the vector at line 113 seems to have no effect but, commenting the vector at line 18 (and the code that writes/read it) solves it somehow.
There’s a catch though, now I get an error from line 297, where after loading all the tiles I sort them. This problem goes away by commenting the for loop at 288-292.
Anyway I still don’t understand why.
I finally found the culprit.
I was storing a XMVECTOR (DirectX Math library) directly in the Tile structure.
The documentation explicitly states that doing so should be avoided.
So what I did is storing a XMFLOAT4 and loading it into a XMVECTOR when the value is needed.
I still don’t understand why it gave access violation but, I suppose is a combination of the internal representation of the vector (in my case, __m128), the optimizations of the math library and the uninitialized move that happens inside std::vector?