我正在使用 DirectX9 通过手动组装地面的顶点来处理一些简单的地形。
在我设置索引的代码部分,我收到了错误:
Windows 在 test.exe 中触发了断点。
这可能是由于堆损坏造成的,这表明 test.exe 或其加载的任何 DLL 中存在错误。
这是我的代码中给我带来问题的部分,我几乎 100% 确定它链接到我的索引指针,但我完成后将其删除......所以我不确定问题是什么。
int total = widthQuads * heightQuads * 6;
DWORD *indices = new DWORD[totalIdx];
for (int y = 0; y < heightQuads; y++)
{
for (int x = 0; x < widthQuads; x++)
{ //Width of nine:
int lowerLeft = x + y * 9;
int lowerRight = (x + 1) + y * 9;
int topLeft = x + (y + 1) * 9;
int topRight = (x + 1) + (y + 1) * 9;
//First triangle:
indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;
//Second triangle:
indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}
d3dDevice->CreateIndexBuffer(sizeof(DWORD)* total, 0, D3DFMT_INDEX16,
D3DPOOL_MANAGED, &groundindex, 0);
void* mem = 0;
groundindex->Lock(0, 0, &mem, 0);
memcpy(mem, indices, total * sizeof (DWORD));
groundindex->Unlock();
delete[] indices;
当我删除这个 block 时,我的程序运行正常。
最佳答案
您给出的代码看起来不错 - 但有一个警告:counter
的初始值不在代码本身中。因此,要么您不从 counter = 0
开始,要么其他一些代码正在破坏您的indices
缓冲区。
这就是堆损坏的美妙之处。无法保证该错误位于代码中已删除的部分中。它可能只是隐藏了代码中其他地方存在的错误。
关于C++堆损坏: Local heap variable causing issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10586570/