I'm having problems loading a binary file to my program.
The file is opened, the data is read. But when it is converted and put to use, the program will stop and give a message:
I have read it has to do with the 'new' and 'delete' operators but I don't see what I'm doing wrong.
I create a new vertex, triangle or color and as soon as I want to give the pointer to the new mesh: The message pops up
my code:
Mesh LoadEG2(char *filename, bool &ReadSucces, bool &ValidFile){ //try to open the file FILE* stream; if(fopen_s(&stream, filename, "rb") != 0) { ReadSucces = false; Mesh m; return m; } ReadSucces = true; //read the header and check if this is a valid one EG2_Header eg2h; fread(&eg2h, sizeof(EG2_Header), 1, stream); if(!IsValidEG2Header(&eg2h)) { ValidFile = false; Mesh m; return m; } ValidFile = true; //allocate some space to read everything std::vector<EGF2_Vertex3> Vertices; std::vector<EGF2_Triangle> Triangles; //the mesh that should be returend Mesh m; //secondly, read all vertices for(unsigned int i = 0; i < eg2h.VertexCount; i++) { EGF2_Vertex3 v; fread(&v, sizeof(EGF2_Vertex3), 1, stream); Vertices.push_back(v); } //then read all the triangles for(unsigned int i = 0; i < eg2h.FaceCount; i++) { EGF2_Triangle t; fread(&t, sizeof(EGF2_Triangle), 1, stream); Triangles.push_back(t); } fclose(stream); //now convert the stuff for(unsigned int i = 0; i < Vertices.size(); i++) { Point3 * col = new Point3(Vertices.R, Vertices.G, Vertices.B); Vertex3 * vert = new Vertex3(Vertices.X, Vertices.Y, Vertices.Z, i); //This is where the problem comes from. m.AddColor(col); m.AddVert(vert); } for(unsigned int i = 0; i < Triangles.size(); i++) { //convert the UV_Coords Point2 *uv1 = new Point2(Triangles.UV[0].X, Triangles.UV[0].Y); Point2 *uv2 = new Point2(Triangles.UV[1].X, Triangles.UV[1].Y); Point2 *uv3 = new Point2(Triangles.UV[2].X, Triangles.UV[2].Y); //Convert the Normal Point3 n; n.X = Triangles.Nor.X; n.Y = Triangles.Nor.Y; n.Z = Triangles.Nor.Z; //convert the triangle and put it together m.AddFace(new Triangle(Triangles.V1, Triangles.V2, Triangles.V3, uv1, uv2, uv3, n)); } //if this is all done, the loading has suceeded return m;}
The mesh class
#ifndef EASX_MESH#define EASX_MESH#include <iostream>#include <vector>#include "EasX3D_Exp.hpp"#include "Point3.hpp"#include "Triangle.hpp"#include "Vertex3.hpp"#include "GraphNode.hpp"class Mesh : GraphNode{ private: std::vector<Vertex3*> Vertices; std::vector<Point3*> Color; std::vector<Triangle*> Triangles; public: Point3 Position; Point3 Scale; Point3 Rotation; bool DoRender; EASX_API Mesh(); EASX_API ~Mesh(); EASX_API void AddVert(Vertex3 *v); EASX_API void AddFace(Triangle *t); EASX_API void AddColor(Point3 *c); EASX_API void InsertVert(Vertex3 *v, unsigned int index); EASX_API void InsertFace(Triangle *t, unsigned int index); EASX_API void InsertColor(Point3 *c, unsigned int index); EASX_API void DeleteVert(unsigned int index); EASX_API void DeleteFace(unsigned int index); EASX_API void DeleteColor(unsigned int index); EASX_API Vertex3* GetVert(unsigned int index); EASX_API Triangle* GetFace(unsigned int index); EASX_API Point3* GetColor(unsigned int index); EASX_API Point3 GetPosition(); EASX_API Point3 GetScale(); EASX_API Point3 GetRotation(); EASX_API void Render();};#endif //EASX_MESH
Some functions from the Mesh class that might have to do with the error:
//constructorMesh::Mesh() : GraphNode(){ Vertices.clear(); Triangles.clear(); Color.clear(); Position.X = Position.Y = Position.Z = 0; Scale.X = Scale.Y = Scale.Z = 0; Rotation.X = Rotation.Y = Rotation.Z = 0; DoRender = true;}//deconstructorMesh::~Mesh(){ for(unsigned int i = 0; i < Vertices.size(); i++) { //delete Vertices; } for(unsigned int i = 0; i < Triangles.size(); i++) { //delete Triangles; } for(unsigned int i = 0; i < Color.size(); i++) { //delete Color; }}//vertex functionsVertex3 *Mesh::GetVert(unsigned int index){ return Vertices[index];}void Mesh::AddVert(Vertex3 *v){ Vertices.push_back(v);}void Mesh::InsertVert(Vertex3 *v, unsigned int index){ Vertices[index] = v;}//face functionsTriangle * Mesh::GetFace(unsigned int index){ return Triangles[index];}void Mesh::AddFace(Triangle* t){ Triangles.push_back(t);}void Mesh::InsertFace(Triangle *t, unsigned int index){ Triangles[index] = t;}//color functionsPoint3 *Mesh::GetColor(unsigned int index){ return Color[index];}void Mesh::AddColor(Point3 * c){ Color.push_back(c);}void Mesh::InsertColor(Point3 * c, unsigned int index){ Color[index] = c;