开发者

C++ Memory Leak, Can't find where

开发者 https://www.devze.com 2022-12-26 18:20 出处:网络
I\'m using Visual Studio 2008, Developing an OpenGL window. I\'ve created several classes for creating a skeleton, one for joints, one for skin, one for a Body(which is a holder for several joints and

I'm using Visual Studio 2008, Developing an OpenGL window. I've created several classes for creating a skeleton, one for joints, one for skin, one for a Body(which is a holder for several joints and skin) and one for reading a skel/skin file.

Within each of my classes, I'm using pointers for most of my data, most of which are declared using = new int[XX]. I have a destructor for each Class that deletes the pointers, using delete[XX].

Within my GLUT display function I have it declaring a body, opening the files and drawing them, then deleting the body at the end of the display. But there's still a memory leak somewhere in the program. As Time goes on, it's memory usage just keep increasing, at a consistent rate, which I'm interpreting as something that's not getting deleted.

I'm not sure if it's something in the glut display function that's just not deleting the Body class, or something else. I've followed the steps for memory leak detection in Visual Studio 2008 and it doesn't report any leak, but I'm not 100% sure if it's working right for me. I'm not fluent in C++, so there maybe something I'm overlooking, can anyone see it?

From main:

v开发者_StackOverflow中文版oid display(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    Body *body = new Body();
    body->readSkel("C:\\skel2.skel");
    body->drawBody();
    body = new Body();
    body->readSkel("C:\\skel1.skel");
    body->drawBody();
    glutSwapBuffers();  
    body->~Body();
    delete body;
}

From Body:

Body::Body(){
    skelFile = string();
    skinFile = string();
    totalJoints = 0;
    joints = new Joint[25];
    skin = new Skin;
}

Body::~Body(){
    delete[25] joints;
    delete skin; 
}


In this code:

Body *body = new Body();
body->readSkel("C:\\skel2.skel");
body->drawBody();
body = new Body();

you're leaking a Body because you don't delete the first one.

And this:

body->~Body();
delete body;

is just weird. You don't explicitly call destructors like that - the delete takes care of calling the destructor.

This code:

delete[25] joints;

is also weird. The correct form is:

delete [] joints;

You're using a non-standard syntax, and the 25 will be ignored. See this question for more information.


Real programmers can write FortranJava in any language! Java requires that you allocate (practically) everything dynamically, but C++ does not.

Since nobody else has pointed it out (at least directly), in display there seems to be no reason to use dynamic allocation at all. Just do something like:

void display(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    Body body;
    body.readSkel("C:\\skel2.skel");
    body.drawBody();

    Body body2;
    body2.readSkel("C:\\skel1.skel");
    body2.drawBody();
    glutSwapBuffers();  
}

If your readSkel clears the existing skeleton data, you don't need to define body2, but without knowing that, this is the easy way to keep things safe.

Likewise, in your definition of Body, you don't seem to be doing anything that requires dynamic allocation either.

class Body { 
    std::string skelFile;
    std::string skinFile;
    int totalJoints;
    Skin skin;
    Joint joints[25];
public:
    Body() : totalJoints(0) {}
};

or better still:

class Body { 
    std::string skelFile;
    std::string skinFile;
    Skin skin;
    std::vector<Joint> joints;
public:
   // presumably other stuff goes here...but you don't need a ctor or dtor.
};

This gets rid of most chances for leaking anything (at least in these parts of the code -- since we haven't seen your Skin or Joint classes, it's hard to guess what they may be doing...


It would help if you'd paste in a little code, but I would:

Double check your syntax: int *foo = new int[size]; delete[] foo;

Ensure all child classes who's parents use dynamic memory also contain destructors, even if the destructor is an empty statement.


Jochen Kalmbach is your friend.

0

精彩评论

暂无评论...
验证码 换一张
取 消