|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 
|
Heap Corruption
I am trying to put together an obj file loader to use with OpenGL. I can compile the code but when It runs VS tells me that the heap is corrupted.
What do you guys think is causing this problem.
Here is my code:
ObjLoader.h
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
struct ObjVertex
{
float x;
float y;
float z;
};
struct ObjNormal
{
float x;
float y;
float z;
};
struct ObjTextCoord
{
float u;
float v;
};
struct ObjTriangle
{
int Vertex[3];
int Normal[3];
int TextCoord[3];
};
class ObjModel
{
public:
ObjModel();
~ObjModel();
ObjModel(const ObjModel& copy);
ObjModel& operator=(const ObjModel& right);
int NumVertex, NumNormal, NumTexCoord, NumTriangle;
ObjVertex* VertexArray;
ObjNormal* NormalArray;
ObjTextCoord* TexCoordArray;
ObjTriangle* TriangleArray;
};
class ObjLoader
{
public:
ObjLoader();
~ObjLoader();
ObjLoader(string file);
void LoadObj(string file);
void FreeObj(void);
ObjModel ReturnObj(void);
protected:
string* fileName;
ObjModel* theObj;
void ReadData(void);
}; ObjLoader.cpp
#include "ObjLoader.h"
ObjModel::ObjModel()
{
ObjModel::NumNormal = 0;
ObjModel::NumTexCoord = 0;
ObjModel::NumTriangle = 0;
ObjModel::NumVertex = 0;
ObjModel::NormalArray = NULL;
ObjModel::TexCoordArray = NULL;
ObjModel::TriangleArray = NULL;
ObjModel::VertexArray = NULL;
}
ObjModel::~ObjModel()
{
if(NormalArray)
delete[] NormalArray;
if(TexCoordArray)
delete[] TexCoordArray;
if(TriangleArray)
delete[] TriangleArray;
if(VertexArray)
delete[] VertexArray;
}
ObjModel::ObjModel(const ObjModel ©)
{
NormalArray = new ObjNormal[copy.NumNormal];
TexCoordArray = new ObjTextCoord[copy.NumTexCoord];
TriangleArray = new ObjTriangle[copy.NumTriangle];
VertexArray = new ObjVertex[copy.NumVertex];
NumNormal = copy.NumNormal;
NumTexCoord = copy.NumTexCoord;
NumTriangle = copy.NumTriangle;
NumVertex = copy.NumVertex;
for(int i = 0; i< NumNormal; i++)
NormalArray[i] = copy.NormalArray[i];
for(int i = 0; i< NumTexCoord; i++)
TexCoordArray[i] = copy.TexCoordArray[i];
for(int i = 0; i< NumTriangle; i++)
TriangleArray[i] = copy.TriangleArray[i];
for(int i = 0; i< NumVertex; i++)
VertexArray[i] = copy.VertexArray[i];
}
ObjModel& ObjModel::operator=(const ObjModel &right)
{
if(NormalArray)
delete[] NormalArray;
if(TexCoordArray)
delete[] TexCoordArray;
if(TriangleArray)
delete[] TriangleArray;
if(VertexArray)
delete[] VertexArray;
NormalArray = new ObjNormal[right.NumNormal];
TexCoordArray = new ObjTextCoord[right.NumTexCoord];
TriangleArray = new ObjTriangle[right.NumTriangle];
VertexArray = new ObjVertex[right.NumVertex];
NumNormal = right.NumNormal;
NumTexCoord = right.NumTexCoord;
NumTriangle = right.NumTriangle;
NumVertex = right.NumVertex;
for(int i = 0; i< NumNormal; i++)
NormalArray[i] = right.NormalArray[i];
for(int i = 0; i< NumTexCoord; i++)
TexCoordArray[i] = right.TexCoordArray[i];
for(int i = 0; i< NumTriangle; i++)
TriangleArray[i] = right.TriangleArray[i];
for(int i = 0; i< NumVertex; i++)
VertexArray[i] = right.VertexArray[i];
return *this;
}
ObjLoader::ObjLoader()
{
fileName = NULL;
theObj = NULL;
}
ObjLoader::~ObjLoader()
{
FreeObj();
}
void ObjLoader::FreeObj(void)
{
if(fileName != NULL)
delete fileName;
if(theObj != NULL)
delete theObj;
}
ObjModel ObjLoader::ReturnObj(void)
{
ObjModel ret(*theObj);
return ret;
}
void ObjLoader::LoadObj(string file)
{
FreeObj();
fileName = new string(file);
theObj = new ObjModel();
ReadData();
}
void ObjLoader::ReadData(void)
{
ifstream input(fileName->c_str());
string buffer;
if(!input.is_open())
return;
while(!input.eof())
{
getline(input,buffer);
if(buffer.substr(0,2) == "vn")
theObj->NumNormal++;
else if(buffer.substr(0,2) == "vt")
theObj->NumTexCoord++;
else if(buffer.substr(0,1) == "v")
theObj->NumVertex++;
else if(buffer.substr(0,1) == "f")
theObj->NumTriangle++;
}
theObj->NormalArray = new ObjNormal[theObj->NumNormal];
theObj->TexCoordArray = new ObjTextCoord[theObj->NumTexCoord];
theObj->TriangleArray = new ObjTriangle[theObj->NumTriangle];
theObj->VertexArray = new ObjVertex[theObj->NumVertex];
input.close();
input.open(fileName->c_str());
input.clear();
if(!input.is_open())
return;
int nC,vC,tC,fC;
nC = vC = tC = fC = 0;
while(!input.eof())
{
getline(input,buffer);
istringstream line(buffer);
string temp;
string f1, f2, f3;
if(buffer.substr(0,2) == "vn")
{
line >> temp >> f1 >> f2 >> f3;
theObj->NormalArray[vC].x = atof(f1.c_str());
theObj->NormalArray[vC].y = atof(f2.c_str());
theObj->NormalArray[vC].z = atof(f3.c_str());
nC++;
}
else if(buffer.substr(0,1) == "v")
{
line >> temp >> f1 >> f2 >> f3;
theObj->VertexArray[vC].x = atof(f1.c_str());
theObj->VertexArray[vC].y = atof(f2.c_str());
theObj->VertexArray[vC].z = atof(f3.c_str());
vC++;
}
else if(buffer.substr(0,1) == "f")
{
line >> temp >> f1 >> f2 >> f3;
int sPos = 0;
int ePos = sPos;
string temp;
ePos = f1.find_first_of("/");
if(ePos != string::npos)
{
temp = f1.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].Vertex[0] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f1.find("/",sPos);
temp = f1.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].Vertex[1] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f1.length();
temp = f1.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].Vertex[2] = atoi(temp.c_str()) - 1;
}
sPos = 0;
ePos = f2.find_first_of("/");
if(ePos != string::npos)
{
temp = f2.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].TextCoord[0] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f2.find("/",sPos);
temp = f2.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].TextCoord[1] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f2.length();
temp = f2.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].TextCoord[2] = atoi(temp.c_str()) - 1;
}
sPos = 0;
ePos = f3.find_first_of("/");
if(ePos != string::npos)
{
temp = f3.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].Normal[0] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f3.find("/",sPos);
temp = f3.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].Normal[1] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f3.length();
temp = f3.substr(sPos,ePos - sPos);
theObj->TriangleArray[fC].Normal[2] = atoi(temp.c_str()) - 1;
}
fC++;
}
}
} test.cpp
#include <iostream>
#include "ObjLoader.h"
using namespace std;
int main(void)
{
ObjModel data;
ObjLoader LoaderClass;
LoaderClass.LoadObj("C:\\cube.obj");
data = LoaderClass.ReturnObj();
cout << "Object Model has: " << data.NumTriangle << " faces \n";
cout << "Verticies" << endl;
for(int i = 0; i < data.NumVertex; i ++)
{
cout << "(" << data.VertexArray[i].x << "," << data.VertexArray[i].y << "," << data.VertexArray[i].z << ")\n";
}
system("PAUSE");
return 0;
} Thanks in advance
|