C++内存泄漏,找不到哪里

发布于 2024-08-28 07:53:23 字数 1068 浏览 6 评论 0原文

我正在使用 Visual Studio 2008,开发 OpenGL 窗口。我创建了几个用于创建骨架的类,一个用于关节,一个用于皮肤,一个用于 Body(它是多个关节和皮肤的支架),一个用于读取 skel/skin 文件。

在我的每个类中,我对大部分数据使用指针,其中大部分是使用 = new int[XX] 声明的。我为每个类都有一个析构函数,使用delete[XX]删除指针。

在我的 GLUT 显示函数中,我让它声明一个主体,打开文件并绘制它们,然后在显示末尾删除主体。但程序中的某个地方仍然存在内存泄漏。随着时间的推移,内存使用量以一致的速度不断增加,我将其解释为不会被删除的东西。

我不确定是否是过剩显示函数中的某些内容只是没有删除 Body 类,或者其他内容。我已按照 Visual Studio 2008 中的内存泄漏检测步骤进行操作,但它没有报告任何泄漏,但我不能 100% 确定它是否适合我。我对 C++ 不太熟悉,所以可能有一些我忽略的东西,有人能看到吗?

从主体:

void 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;
}

从主体:

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

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

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:

void 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; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花伊自在美 2024-09-04 07:53:23

在此代码中:

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

您泄漏了 Body,因为您没有删除第一个。

body->~Body();
delete body;

很奇怪。您不需要像这样显式调用析构函数 - delete 负责调用析构函数。

这段代码:

delete[25] joints;

也很奇怪。正确的形式是:

delete [] joints;

您使用的是非标准语法,25 将被忽略。有关详细信息,请参阅此问题

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.

伏妖词 2024-09-04 07:53:23

真正的程序员可以用任何语言编写FortranJava! Java 要求您动态分配(实际上)所有内容,但 C++ 不需要。

由于没有其他人指出(至少直接指出),因此在 display 中似乎根本没有理由使用动态分配。只需执行以下操作:

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();  
}

如果您的 readSkel 清除了现有的骨架数据,则无需定义 body2,但在不知道的情况下,这是保留内容的简单方法安全的。

同样,在您对 Body 的定义中,您似乎也没有做任何需要动态分配的事情。

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

或者更好的是:

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.
};

这消除了大多数泄漏任何内容的机会(至少在代码的这些部分 - 因为我们还没有看到您的 Skin 或 Joint 类,所以很难猜测它们可能在做什么......

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

不必了 2024-09-04 07:53:23

如果您粘贴一些代码将会有所帮助,但我会:

仔细检查您的语法: int *foo = new int[size];删除[] foo;

确保父类使用动态内存的所有子类也包含析构函数,即使析构函数是空语句。

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.

稚然 2024-09-04 07:53:23

Jochen Kalmbach 是您的朋友。

Jochen Kalmbach is your friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文