tags:

views:

124

answers:

3

Hi all, 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_SMOOTH);      
  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.

+3  A: 

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.

Patrice Bernassola
The timer is deleted automatically, as the child of QGLWidget, which is a QObject. And as i understand, if i'm working with Ren as QGLWidget, then the QGLWidget should have a virtual destructor. If so, when deleting Ren, as QGLWidget the Ren destructor will be called instead of the destructor of QGLWidget. And memory is leaking permanently, while the program is running
Andrew
You are right, I did not see the "this" as constructor parameter. I will edit my answer.
Patrice Bernassola
Ren's base class has a virtual destructor, so you don't have to declare any destructor in this class at all. See http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 And the question is about leaks *before* the destructors are called.
goodrone
A: 

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.

Wolfgang Plaschg
A: 

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.

Andrew