0
votes

I called the following function in a MEX file (matlab) in a loop. It causes memory leak in glColor3f. The memory continue to increase. Is there any idea to solve it?

static GLuint createDisplayListWithColor(double *fM, int fNum, double *vM, int vNum, GLfloat *ColorM, int colorNum)
{
    GLuint theShape;
    int i;
    double *fp;
    int vIndex, fNum2, vNum2;

    fNum2 = fNum * 2;
    vNum2 = vNum * 2;

    theShape = glGenLists (1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glNewList(theShape, GL_COMPILE);
    glBegin (GL_TRIANGLES);


        for (i = 1; i <= fNum; i++) {
            fp = fM + i-1;
            vIndex = (int)fp[0] - 1;
            glColor3f(ColorM[vIndex], ColorM[vIndex + vNum], ColorM[vIndex + vNum2]);
            glVertex3d(vM[vIndex], vM[vIndex + vNum], vM[vIndex + vNum2] );

            vIndex = (int)fp[fNum] - 1;
            glColor3f(ColorM[vIndex], ColorM[vIndex + vNum], ColorM[vIndex + vNum2]);
            glVertex3d(vM[vIndex], vM[vIndex + vNum], vM[vIndex + vNum2] );

            vIndex = (int)fp[fNum2] - 1;
            glColor3f(ColorM[vIndex], ColorM[vIndex + vNum], ColorM[vIndex + vNum2]);
            glVertex3d(vM[vIndex], vM[vIndex + vNum], vM[vIndex + vNum2] );
               }

    glEnd ();
    glutSwapBuffers();
    glEndList();
    return theShape;
}
1

1 Answers

2
votes

Well, given the fact that each almost every OpenGL command it recorded in a Display List it's no surprise that a calling OpenGL commands in a loop within a Display List compilation will consume memory. Use glDeleteLists before program termination to delete all lists.

But honestly, why are you using Display Lists in the first place? Display Lists and Immediate Mode are deprecated. Even almost 10 years ago with the introduction of OpenGL-2 it was considered to remove them, and it was strongly recommended not to use them in new code.