C++内存泄漏,找不到哪里
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在此代码中:
您泄漏了
Body
,因为您没有删除第一个。这
很奇怪。您不需要像这样显式调用析构函数 -
delete
负责调用析构函数。这段代码:
也很奇怪。正确的形式是:
您使用的是非标准语法,
25
将被忽略。有关详细信息,请参阅此问题。In this code:
you're leaking a
Body
because you don't delete the first one.And this:
is just weird. You don't explicitly call destructors like that - the
delete
takes care of calling the destructor.This code:
is also weird. The correct form is:
You're using a non-standard syntax, and the
25
will be ignored. See this question for more information.真正的程序员可以用任何语言编写
FortranJava! Java 要求您动态分配(实际上)所有内容,但 C++ 不需要。由于没有其他人指出(至少直接指出),因此在
display
中似乎根本没有理由使用动态分配。只需执行以下操作:如果您的
readSkel
清除了现有的骨架数据,则无需定义body2
,但在不知道的情况下,这是保留内容的简单方法安全的。同样,在您对 Body 的定义中,您似乎也没有做任何需要动态分配的事情。
或者更好的是:
这消除了大多数泄漏任何内容的机会(至少在代码的这些部分 - 因为我们还没有看到您的 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:If your
readSkel
clears the existing skeleton data, you don't need to definebody2
, 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.
or better still:
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...
如果您粘贴一些代码将会有所帮助,但我会:
仔细检查您的语法: 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.
Jochen Kalmbach 是您的朋友。
Jochen Kalmbach is your friend.