When I goto define a varible in a for loop the first time everything goes fine the second time around my program crashes with
'Segmentation fault' showing in konsole.
meshes[i].vertices = (Vertice*)malloc(sizeof(Vertice) * meshes[i].mesh.numvertexs * meshes[i].mesh.nummeshframes);
If you need to look at the hole funtion and the structs.
typedef struct Vertice
{
signed short vec[3];
unsigned char normal[2];
};
typedef struct Mesh
{
MeshHeader mesh;
Skin* skins;
Triangle* triangles;
TexCoord* texcoord;
Vertice* vertices; //this needs clearing out now
TEXTURE texture;
unsigned char settexture;
};
bool LoadModel(char* FileName)
{
FILE* file;
int i;
file = fopen(FileName, "rb");
if(!file)
{
fprintf(stderr, "Cannt find file %s!!!!\n", FileName);
return false;
}
md3name = (char*)malloc(strlen(FileName));
strcpy(md3name, FileName);
fread(&header, 1, sizeof(Header), file);
if((header.id[0] != 'I' || header.id[1] != 'D' || header.id[2] != 'P' || header.id[3] != '3') || header.version != 15)
{
fprintf(stderr, "Invalid file format not version 15 or ID not IDP3!!!\n");
return 0;
}
boneframes = (BoneFrame*)malloc(sizeof(BoneFrame) * header.numboneframes);
fread(boneframes, sizeof(BoneFrame), header.numboneframes, file);
tags = (Tag*)malloc(sizeof(Tag) * header.numboneframes * header.numtags);
fread(tags, sizeof(Tag), header.numboneframes * header.numtags, file);
links = (NuclearMD3**)malloc(sizeof(NuclearMD3) * header.numtags);
for(i = 0; i < header.numtags; i++)
{
links[i] = 0;
}
meshes = (Mesh*)malloc(sizeof(Mesh) * header.nummeshes);
long meshoffset = ftell(file);
for(i = 0; i < header.nummeshes; i++)
{
fseek(file, meshoffset, SEEK_SET);
fread(&meshes[i].mesh, sizeof(MeshHeader), 1, file);
fseek(file, meshoffset + meshes[i].mesh.tristart, SEEK_SET);
meshes[i].triangles = (Triangle*)malloc(sizeof(Triangle) * meshes[i].mesh.numtriangles);
fread(meshes[i].triangles, sizeof(Triangle), meshes[i].mesh.numtriangles, file);
fseek(file, meshoffset + meshes[i].mesh.texvecstart, SEEK_SET);
meshes[i].texcoord = (TexCoord*)malloc(sizeof(TexCoord*) * meshes[i].mesh.numvertexs);
fread(meshes[i].texcoord, sizeof(TexCoord), meshes[i].mesh.numvertexs, file);
fseek(file, meshoffset + meshes[i].mesh.vertexstart, SEEK_SET);
meshes[i].vertices = (Vertice*)malloc(sizeof(Vertice) * meshes[i].mesh.numvertexs * meshes[i].mesh.nummeshframes); //the buggy code
fread(meshes[i].vertices, sizeof(Vertice), meshes[i].mesh.nummeshframes * meshes[i].mesh.numvertexs, file);
meshoffset += meshes[i].mesh.meshsize;
}
fclose(file);
header.numboneframes -= 1;
startframe = 0;
endframe = header.numboneframes;
return 1;
}
If you want to just look at the hole class you can go
here.
[edited by - nukem on December 8, 2002 5:05:57 PM]
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind