Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Dec 27th, 2006, 12:00 AM   #1
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Thumbs down 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 &copy)
{
    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
__________________

Wizard1988 is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
25 ways to efficiently sort an array/list of integers Jimbo Software Design and Algorithms 36 Mar 3rd, 2008 7:31 AM
Heap vs. Stack memory Eric the Red C++ 11 Oct 24th, 2006 7:18 PM
new thread for min heap cwl157 C++ 22 Apr 7th, 2006 12:22 PM
adding printing deleting values from a min heap cwl157 C++ 15 Mar 24th, 2006 12:52 AM
When to use the new keyword in C++? titaniumdecoy C++ 28 Mar 16th, 2006 1:36 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC