Domanda

I try to create a simple class store my model variables vertex array and vertexbuffer. So I create an array of myclass to create and manage object dynamically. But when

hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &pieces[counter].g_pVertexBuffer);

this line run I get the error "Access violation reading location". I try to many things but I never success. If I don't use class and array I don't get this error. Porgram runs without error.

MyClass:

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"

and using objects of 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;
}

When I get access violation counter is 0.

Source code of my project: http://1drv.ms/1nKdoUf

È stato utile?

Soluzione

The access violation exception comes when you are trying to access a location of memory which is not allocated by your program. I looked at your project and found that in function InitDevice you are allocating an array and assigning it to pieces vertices line no 747

pieces[counter].vertices = new SimpleVertex[vertexCount];

in the above code you are using vertexCount which was assigned while reading the chessBoard.txt and which is always 6 (as defined in the file). Whereas you need to use the vertex count read from the current file as soon as I did use the current vertexCount (i.e., pieces[counter].vertexCount) your program started working and the chess board was visible. Try out changing the above code statement to this

pieces[counter].vertices = new SimpleVertex[pieces[counter].vertexCount];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top