2012-10-20 71 views
0

我得到這個錯誤,我無法弄清楚的原因。它是類型問題? 錯誤在行上:
「size = objmesh-> n_objvertexdata * sizeof(vec3)* sizeof(vec3);」
不知道它是什麼,一直在擺弄它。幫幫我?EXC_BAD_ACCESS,OPENGL ES

objmesh = &objmesh[0]; 

unsigned char *vertex_array = NULL, 
*vertex_start = NULL; 
unsigned int i = 0, index = 0, stride = 0, size = 0; 


size = objmesh->n_objvertexdata * sizeof(vec3) * sizeof(vec3); //this lines gives error 

vertex_array = (unsigned char *) malloc(size); 
vertex_start = vertex_array; 

while (i != objmesh->n_objvertexdata) { 
    index = objmesh->objvertexdata[i].vertex_index; 

    memcpy(vertex_array, &obj->indexed_vertex[index], sizeof(vec3)); 
    vertex_array += sizeof(vec3); 

    memcpy(vertex_array, &obj->indexed_normal[index], sizeof(vec3)); 

    vertex_array += sizeof(vec3); 
    ++i; 

回答

0

崩潰可能是因爲objmesh是一個無效的指針(NULL或只是懸掛指針)。而當你訪問相對於無效指針的內存時,你會得到崩潰。

或者這是另一個原因。據我所知objmesh是一些容器,它包含一些數據,並且它的大小。當你這樣做

objmesh = &objmesh[0]; 

objmesh不再是容器,它是它所擁有的數據。所以你應該使用另一個變量進行賦值。

+0

你有一半的權利,用objmesh =&obj-> objmesh [0]修復它;在第一行,它指向了錯誤的對象。 – user1191343