【发布时间】:2014-05-13 18:08:39
【问题描述】:
我尝试创建一个简单的类来存储我的模型变量顶点数组和顶点缓冲区。所以我创建了一个 myclass 数组来动态创建和管理对象。但是当
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &pieces[counter].g_pVertexBuffer);
这一行运行我得到错误“访问冲突读取位置”。我尝试了很多事情,但我从未成功。 如果我不使用类和数组,我不会收到此错误。程序运行没有错误。
我的班级:
class Piece
{
public:
double positionX, positionZ,
red, green,blue;
bool renderable;
int type, color, vertexCount;
XMMATRIX g_WorldPieces;
ID3D11Buffer* g_pVertexBuffer;
SimpleVertex* vertices;
Piece();
void create(int, int, double, double);
};
Piece::Piece()
{
g_pVertexBuffer = NULL;
}
void Piece::create(int t, int c, double pX, double pZ)
{
g_pVertexBuffer = NULL;
renderable = true;
type = t;
color = c;
red=green=blue=0.0f;
if(color == 1)
red=green=blue=1.0f;
positionX = pX;
positionZ = pZ;
vertexCount = 0;
}
Piece *pieces;//Global variable which define after defining "ID3D11Device* g_pd3dDevice = NULL"
并使用 myclass 的对象:
for(int x=0;x<4;x++)
{
if(x==2)
{
c=0;
positionZ = 12.5;
}
for(int y=0;y<8;y++)
{
pieces[counter].create(typeArray[x][y],c,positionX,positionZ);
positionX += 5;
switch(pieces[counter].type)
{
//Switching object txt. All cases and breaks are fine.
}
fin >> pieces[counter].vertexCount;
pieces[counter].vertices = new SimpleVertex[vertexCount];
for(int i=0; i<vertexCountpiyon; i++)
{
fin >> pieces[counter].vertices[i].Pos.x >> pieces[counter].vertices[i].Pos.y >> pieces[counter].vertices[i].Pos.z;
fin >> pieces[counter].vertices[i].Tex.x >> pieces[counter].vertices[i].Tex.y;
fin >> pieces[counter].vertices[i].Normal.x >> pieces[counter].vertices[i].Normal.y >> pieces[counter].vertices[i].Normal.z;
}
fin.close();
bd.ByteWidth = sizeof( SimpleVertex ) *pieces[counter].vertexCount;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = pieces[counter].vertices;
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &pieces[counter].g_pVertexBuffer);//THIS LINE IS MY PROBLEM!
if( FAILED( hr ) ) return hr;
counter++;
}
positionX = -17.5;
positionZ += 5;
}
当我得到访问冲突计数器为 0 时。
我的项目源码:http://1drv.ms/1nKdoUf
【问题讨论】:
-
您是否能够推断出
bd、InitData和g_pVertexBuffer这三个变量中的哪一个导致了问题?我假设g_pd3dDevice被设置为一个实例? -
当我使用我的类的对象数组时,只是这个变量 g_pVertexBuffer 会导致问题。如果我对每个模型都使用简单的变量,我不会收到任何错误。
标签: c++ arrays directx access-violation directx-11