Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 26th, 2006, 11:00 PM   #1
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Dec 27th, 2006, 1:13 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Can you post a sample data file so I can test it?

Also note: .obj is a bad choice for an extension - it is already used for the compiler output.
The Dark is offline   Reply With Quote
Old Dec 27th, 2006, 1:38 AM   #3
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
That would be the standard 3D data format and I thought '.o' was used for object files? At least in the character crunched unix world it is.
Game_Ender is offline   Reply With Quote
Old Dec 27th, 2006, 2:47 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
All sorts of problems in the original code. Some related to the "heap corruption", some not.

1) Never employ "using namespace std;" in a header file. In a header file, you usually need to suspend your laziness, and prefix anything in namespace std with "std::".

2) Your ObjModel implementation relies on a technique known as "lazy initialisation" --- eg pointers set to NULL, and then set to something useful later on. Since you are doing that with multiple pointers, it is an opportunity to forget to set one or more pointers correctly later on.

3) Make the members of ObjModel private. And then provide public member functions that can be used by ObjLoader. Yes, that will mean ObjLoader will not compile (unless you use the member functions). But it gives a means of ensuring that everything that changes an ObjModel is part of ObjModel --- and can therefore enforce any requirements (such as ensuring data is allocated before using it).

4) "delete pointer" (or "delete [] pointer") does not need to be protected with a test of "pointer != NULL", unless you are using a compiler that is very old (probably more than 15 years old).

5) To understand why you are getting heap corruption, let's have a look at some code of yours.....
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];
I've cut off the end of the function. But, if this function is called twice, as in;
int main()
{
     // assume SomeLoader is an instance of ObjLoader
    SomeLoader.ReadData();
    SomeLoader.ReadData();
}
it will cause a memory leak, because the first call dynamically allocates memory and the second call dynamically allocates more memory without releasing the memory that was allocated in the first call. Some primitive memory analysers describe that as a heap corruption ... There is a basic rule of thumb you have violated: when reallocating memory, you need to release the old memory.
grumpy is offline   Reply With Quote
Reply

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 6:31 AM
Heap vs. Stack memory Eric the Red C++ 11 Oct 24th, 2006 6:18 PM
new thread for min heap cwl157 C++ 22 Apr 7th, 2006 11:22 AM
adding printing deleting values from a min heap cwl157 C++ 15 Mar 23rd, 2006 11:52 PM
When to use the new keyword in C++? titaniumdecoy C++ 28 Mar 16th, 2006 12:36 PM




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

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