PDA

View Full Version : gluQuadric Functions & Memory Leaks



Carmine
11-11-2015, 10:04 AM
The snippet of code below appears in another post on the forum.
As used below, would the glu functions result in a memory leak?
Would declaring qobj to be 'static' be a workaround?
Thanks.


void drawcannon()
{
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
qobj = gluNewQuadric();
gluQuadricNormals(qobj, GLU_SMOOTH);
gluCylinder(qobj, 1.0, 1.0, 0.4, 10, 16);
......
}

GClements
11-11-2015, 11:17 AM
The snippet of code below appears in another post on the forum.
As used below, would the glu functions result in a memory leak?

Yes. In this case, the quadric object should be deleted with gluDeleteQuadric(). Alternatively ...



Would declaring qobj to be 'static' be a workaround?

The same quadric could be used for the lifetime of the program, with the pointer stored in a global variable or "static" local variable. E.g.


static GLUquadric *qobj;

if (!qobj) {
qobj = gluNewQuadric();
gluQuadricNormals(qobj, GLU_SMOOTH);
}

gluCylinder(qobj, 1.0, 1.0, 0.4, 10, 16);