2014-05-13 7 views
0

내 모델 변수 정점 배열과 정점 버퍼를 저장하는 간단한 클래스를 만들려고합니다. 그래서 나는 객체를 동적으로 생성하고 관리하기 위해 myclass 배열을 생성한다. 하지만 언제ID3D11Buffer로 인해 액세스 위반이 발생했습니다.

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

이 라인 실행 "Access violation reading location"오류가 발생합니다. 나는 많은 것을 시도하지만 결코 성공하지 못합니다. 클래스 및 배열을 사용하지 않으면이 오류가 발생하지 않습니다. Porgram은 오류없이 실행됩니다.

MyClass에 : 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" 

및 사용하여 객체 :

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

+0

'bd','InitData' 및'g_pVertexBuffer'의 세 변수 중 어느 것이 문제를 일으키는 지 추측 할 수 있었습니까? 나는'g_pd3dDevice'가 인스턴스로 설정되어 있다고 가정합니까? – MicroVirus

+0

이 변수 g_pVertexBuffer를 사용하면 클래스의 객체 배열을 사용할 때 문제가 발생합니다. 모든 모델에 간단한 varable을 사용하면 오류가 발생하지 않습니다. –

답변

1

액세스 위반 예외는 lo 액세스하려고 할 때 발생합니다. 귀하의 프로그램에 의해 할당되지 않은 메모리 양이온. 나는 당신의 프로젝트 보았고 chessBoard.txt을 읽는 동안 할당이 vertexCount를 사용하는 위의 코드에 더 747

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

을 기능 InitDevice 당신이 배열을 할당되지 않고 조각 정점에 할당 줄 것을 발견 이 값은 항상 6입니다 (파일에 정의 된대로). 현재 vertexCount (예 : pieces [counter] .vertexCount)를 사용하자마자 현재 파일에서 읽은 버텍스 카운트를 사용해야하지만 프로그램이 작동하기 시작하고 체스 보드가 표시됩니다. 위 코드를 다음과 같이 변경해보십시오.

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

제 친구에게도 감사합니다. 바로 vertexCountPiyon 대신 [카운터] .vertexCount를 사용해야합니다. –

+0

환영의 꽃 봉오리. 당신의 재료로 행운을 빌어 요 !! –