Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Heap Corruption (http://www.programmingforums.org/showthread.php?t=12263)

Wizard1988 Dec 27th, 2006 12:00 AM

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.:confused:

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

The Dark Dec 27th, 2006 2:13 AM

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.

Game_Ender Dec 27th, 2006 2:38 AM

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.

grumpy Dec 27th, 2006 3:47 AM

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.


All times are GMT -5. The time now is 1:27 AM.

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