开发者

Memory leak in a simple opengl program

I've wrote a simple opengl program to make some test. Here is the program:

#include <QApplication>
#include <QGLWidget>
#include <QTimer>
#include <glut.h>

class Ren : public QGLWidget
{
public:
 Ren() : QGLWidget() 
 {
  timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()),
   this, SLOT(updateGL()));
 }

 void startUpdateTimer()
 {
  timer->start(40);
 }

 void initializeGL()
 {
  glShadeModel(GL_S开发者_开发问答MOOTH);      
  glClearColor(0.5f, 0.5f, 0.5f, 0.0f);     
  glClearDepth(1.0f);       
  glEnable(GL_DEPTH_TEST);   
 }

 void resizeGL(int width, int height)
 {
  if(height == 0){
   height = 1;
  }
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  GLfloat aspectRatio = (GLfloat)width / (GLfloat)height;
  gluPerspective(60.0, aspectRatio, 0.01, 10000.0);
  glMatrixMode(GL_MODELVIEW);
 }

 void paintGL()
 {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);

  glColor3d(1, 0, 0);
  glutSolidCube(0.3);
 }

 QTimer *timer;
};

int main(int argc, char **argv)
{
 QApplication app(argc, argv);

 Ren r;
 r.show();
 r.startUpdateTimer();

 return app.exec();
}

The problem is that the application is leaking memory, when timer is active. For leak detection I used windows task manager.


Since Rend is a subclass you must declare a virtual destructor. Otherwise you have memory leaks and you can have heap corruption if you delete your Ren object while using it as QGLObject.

Edit: Removed part:

In the Constructor, your are allocating memory for the timer but you never release it. You need to delete the timer pointer.


Sorry. I was wrong. There is now leaking. The 'leaking' is stopped after some time (about a minute). I think it's a kind of opengl or Qt job. I've looked some Qt examples and saw the same thing in Textures example. The same thing is happened, if I change another examples to draw something else in PaintGL() function (depending on it there is different 'leaking' time.


Please give us more detail about how much memory your program is leaking. I highly doubt that your code is leaking because you're just allocating one QTimer object. Even if you don't delete your timer this wouldn't be a problem, because the OS releases the memory anyway. This is ugly, of course, but not leaking in a strict sense.

If the allocated memory grows steadily over time, then there's a memory leak. If that's the case, it's not your code's fault, since there's simply nothing you allocate except the timer once.


GLUT primitives are supposed to be used in GLUT programs running the GLUT main loop which cleans up quadrics allocated by the primitives, by calling GLUT primitives outside of a GLUT program you bypass the clean-up tasks and then you have leaked memory.

If you add used a memory leak detection program - Valgrind on Windows or Instruments on the Mac - you would have seen that the leaked blocks come from gluNewQuadric. Remove the call to glutSolidCude and the leak will disappear.

An easy solution is to use Free GLUT primitives instead of the whole GLUT shipping with your OS.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜