Original Post
As usual i am having problems with dynamic memory. I made my own loader for OBJ files. It compiles without errors, but i get the usual segmentation fault. I have debugged it. And it turns out that when the reader tries to read in a float to members of a dynamic CVector3 array, it crashes. I have posted some code below, and marked important points:
bool CIrisOBJmodel::LoadOBJ(LPSTR strName)
{
if(!strName){
return false;
IrisLogger.Write("-1");}
//These are member pointers defined in the .h file
Verticies = NULL;
Normals = NULL;
UVcoords = NULL;
Faces = NULL;
VertArray = NULL;
NormalArray = NULL;
UVcoordArray = NULL;
pFile = fopen(strName,"r");
if(!pFile){
return false;
IrisLogger.Write("0");}
//Count the amount of verts, normals, etc
//This counter works correctly, i have checked it
CountLines();
//Assign memory for stuff
//The numVert... etc are the correct value
Verticies = new CVector3[numVerticies];
Normals = new CVector3[numNormals];
UVcoords = new CVector2[numUVcoords];
Faces = new CFace3[numFaces];
VertArray = new CVector3[numVerticies];
NormalArray = new CVector3[numNormals];
UVcoordArray = new CVector2[numUVcoords];
IrisLogger.Write("1");
//Read it in
//Debugger crashes in this ReadFile() function
ReadInFile();
//Pack into vertex arrays
PackIntoArray();
//close file
fclose(pFile);
return true;
}
void CIrisOBJmodel::ReadInFile()
{
char strline[255];
char ch;
char nch;
int X, xv=1, xt=1, xn=1, xf=1;
//while file not read through
//The whole while, switch loop works correctly
for(X= 0;X<=numLines;X++)
{
ch = fgetc(pFile);
switch(ch)
{
case 'v':
//Read in some type of vertex
//get another char
nch = fgetc(pFile);
if(nch==' ')
{
//its a vert
//The debbugger crashes here, when floats are read into arrays
fscanf(pFile,"%f %f %f\n",Verticies[xv].x,Verticies[xv].y,Verticies[xv].z);
++xv;
}
else if(nch=='t')
{
//its a UVcoord
fscanf(pFile,"%f %f\n",UVcoords[xt].x,UVcoords[xt].y);
++xt;
}
else
{
//its a normal
fscanf(pFile,"%f %f %f\n",Normals[xn].x,Normals[xn].y,Normals[xn].z);
++xn;
}
fscanf(pFile,"%s\n",strline);
break;
case 'f':
//Its a face
//for OBJ files, the CVector3's are used as an index
//Order is vert/UVcoord/normal
fscanf(pFile,"%f/%f/%f %f/%f/%f %f/%f/%f\n",Faces[X].vx,Faces[X].tx,Faces[X].nx,
Faces[X].vy,Faces[X].ty,Faces[X].ny,
Faces[X].vz,Faces[X].nz);
break;
default:
//Probably a comment or something, next line
fgets(strline, 100, pFile);
break;
}
}
}