Permalink
Browse files

2005-06-15 Al Riddoch <alriddoch@zepler.org>

	* libmd3/loader.c: Don't free mesh when it fails to load. Free all
	  the meshes as that is how they were allocated.

	* libmd3/loader.c, libmd3/loader.h: Add libmd3_file_free() to
	  free the entire structure.

	* tests/dumpmd3.c: Test that libmd3_file_free works.

	* configure.ac: Increment interface version.
  • Loading branch information...
1 parent 731dbb0 commit d556088fae35f5477b1881761d6c70c5efcc6f13 @alriddoch committed Jun 15, 2005
Showing with 47 additions and 17 deletions.
  1. +12 −0 ChangeLog
  2. +3 −0 TODO
  3. +1 −1 configure.ac
  4. +28 −16 libmd3/loader.c
  5. +1 −0 libmd3/loader.h
  6. +2 −0 tests/dumpmd3.c
View
@@ -1,3 +1,15 @@
+2005-06-15 Al Riddoch <alriddoch@zepler.org>
+
+ * libmd3/loader.c: Don't free mesh when it fails to load. Free all
+ the meshes as that is how they were allocated.
+
+ * libmd3/loader.c, libmd3/loader.h: Add libmd3_file_free() to
+ free the entire structure.
+
+ * tests/dumpmd3.c: Test that libmd3_file_free works.
+
+ * configure.ac: Increment interface version.
+
2005-06-13 Al Riddoch <alriddoch@zepler.org>
* libmd3/convert.c, libmd3/convert.h: Fix the normal extraction
View
3 TODO
@@ -23,3 +23,6 @@ Extract normals:
Implement freeing models.
Check for an use sinf and cosf if available.
+
+Freeing mesh in libmd3_mesh_load is bad, because it wasn't allocated
+as a mesh.
View
@@ -15,7 +15,7 @@ AM_INIT_AUTOMAKE([dist-bzip2 dist-zip check-news])
dnl Initialise automake
LIBMF_VERSION=$VERSION
-LIBMF_INTERFACE_VERSION='-version-info 0:0:0'
+LIBMF_INTERFACE_VERSION='-version-info 1:0:0'
AC_SUBST(LIBMF_VERSION)
AC_SUBST(LIBMF_INTERFACE_VERSION)
View
@@ -110,21 +110,18 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
file_pos = ftell(fptr);
if (file_pos == -1) {
fprintf(stderr, "Unexpected error reading start pos of mesh.\n");
- free(mesh);
return 1;
}
mesh->mesh_header = calloc(1, sizeof(md3_mesh));
if (mesh->mesh_header == NULL) {
perror("calloc");
- free(mesh);
return 1;
}
if (fread(mesh->mesh_header, sizeof(md3_mesh), 1, fptr) != 1) {
fprintf(stderr, "Unexpected end of file.\n");
free(mesh->mesh_header);
- free(mesh);
return 1;
}
@@ -138,22 +135,19 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
mesh->mesh_header->ident[2],
mesh->mesh_header->ident[3]);
free(mesh->mesh_header);
- free(mesh);
return 1;
}
if (fseek(fptr, file_pos + mesh->mesh_header->skin_start, SEEK_SET) != 0) {
fprintf(stderr, "Unexpected error finding start of skins.\n");
free(mesh->mesh_header);
- free(mesh);
return 1;
}
mesh->skins = calloc(mesh->mesh_header->skin_count, sizeof(md3_skin));
if (mesh->skins == NULL) {
perror("calloc");
free(mesh->mesh_header);
- free(mesh);
return 1;
}
@@ -163,15 +157,13 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
fprintf(stderr, "Unexpected end of file.\n");
free(mesh->mesh_header);
free(mesh->skins);
- free(mesh);
return 1;
}
if (fseek(fptr, file_pos + mesh->mesh_header->triangle_start, SEEK_SET) != 0) {
fprintf(stderr, "Unexpected error finding start of tringles.\n");
free(mesh->mesh_header);
free(mesh->skins);
- free(mesh);
return 1;
}
@@ -181,7 +173,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
perror("calloc");
free(mesh->mesh_header);
free(mesh->skins);
- free(mesh);
return 1;
}
@@ -191,7 +182,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
free(mesh->mesh_header);
free(mesh->skins);
free(mesh->triangles);
- free(mesh);
return 1;
}
@@ -200,7 +190,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
free(mesh->mesh_header);
free(mesh->skins);
free(mesh->triangles);
- free(mesh);
return 1;
}
@@ -211,7 +200,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
free(mesh->mesh_header);
free(mesh->skins);
free(mesh->triangles);
- free(mesh);
return 1;
}
@@ -222,7 +210,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
free(mesh->skins);
free(mesh->triangles);
free(mesh->texcoords);
- free(mesh);
return 1;
}
@@ -232,7 +219,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
free(mesh->skins);
free(mesh->triangles);
free(mesh->texcoords);
- free(mesh);
return 1;
}
@@ -246,7 +232,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
free(mesh->skins);
free(mesh->triangles);
free(mesh->texcoords);
- free(mesh);
return 1;
}
@@ -258,7 +243,6 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
free(mesh->triangles);
free(mesh->texcoords);
free(mesh->vertices);
- free(mesh);
return 1;
}
@@ -296,11 +280,13 @@ static int libmd3_meshes_load(FILE * fptr, libmd3_file * file)
for(i = 0; i < file->header->mesh_count; ++i, ++meshp) {
if (libmd3_mesh_load(fptr, meshp)) {
fprintf(stderr, "Unexpected error reading mesh %d\n", i);
+ free(meshes);
return 1;
}
file_pos += meshp->mesh_header->mesh_len;
if (fseek(fptr, file_pos, SEEK_SET) != 0) {
fprintf(stderr, "Unexpected error seeking to mesh %d\n", i + 1 + 1);
+ free(meshes);
return 1;
}
}
@@ -363,5 +349,31 @@ libmd3_file * libmd3_file_load(const char * filename)
libmd3_tag_load(fptr, file);
libmd3_meshes_load(fptr, file);
+ fclose(fptr);
+
return file;
}
+
+void libmd3_file_free(libmd3_file * file)
+{
+ int i;
+ libmd3_mesh * mesh;
+
+ free(file->frames);
+ free(file->tags);
+ mesh = file->meshes;
+ for (i = 0; i < file->header->mesh_count; ++i, ++mesh) {
+ free(mesh->mesh_header);
+ free(mesh->skins);
+ free(mesh->triangles);
+ free(mesh->texcoords);
+ free(mesh->vertices);
+ if (mesh->normals != NULL) {
+ free(mesh->normals);
+ }
+ }
+
+ free(file->meshes);
+ free(file->header);
+ free(file);
+}
View
@@ -37,5 +37,6 @@ typedef struct _libmd3_file {
} libmd3_file;
libmd3_file * libmd3_file_load(const char * filename);
+void libmd3_file_free(libmd3_file * file);
#endif /* LIBMD3_LOADER_H */
View
@@ -193,5 +193,7 @@ int main(int argc, char *argv[])
dump_frames(file);
dump_meshes(file);
+ libmd3_file_free(file);
+
return 0;
}

0 comments on commit d556088

Please sign in to comment.