#159 freeglut - valgrind memory issue - glutMainLoopEvent

open
nobody
moderate (59)
5
2011-12-10
2011-12-10
clemens
No

dear all,
i am having a little bit of trouble with exiting freeglut properly.

first of all, I have to report a bug: if I define a function

void close(void)

this function will be executed when closing the window, EVEN if I did NOT gave freeglut the callback glutCloseFunc(&close) ! renaming it with "closeme" solved the problem.

then, I am looking for a way to exit my program properly.

I tried:

while(continue_in_main_loop) glutMainLoopEvent();

and continue_in_main_loop is a global variable, by pressing esc, it is set to 0. but it turned out that the

glutLeaveMainLoop();

gives the minimum number of memory leaks (tested with valgrind). here is the code:

#include <stdio.h>

#include <stdlib.h>

#include <GL/freeglut.h>

void keyboard(unsigned char key, const int x, const int y);

void display(void);

void closeme(void);

static unsigned char continue_in_main_loop=1;

int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutCreateWindow("GLUT Test");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutKeyboardFunc(&keyboard);

glutDisplayFunc(&display);

//glutCloseFunc(&closeme);

glutMainLoop();
//while(continue_in_main_loop) glutMainLoopEvent();

printf("we're out\n");

return EXIT_SUCCESS;

}

void closeme(void)

{

printf("we're out too\n");
glutLeaveMainLoop();
continue_in_main_loop=0;

}

void keyboard(unsigned char key, const int x, const int y)

{

switch (key)

{

case 27:

printf("we're here\n");
glutLeaveMainLoop();
continue_in_main_loop=0;

break;

}

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.,0.,0.);

glBegin(GL_POLYGON);

glVertex2f(-.5,-.5);

glVertex2f(.5,-.5);

glVertex2f(.5,.5);

glVertex2f(-.5,.5);

glEnd();

glFlush();
glutSwapBuffers();

}

the result of valgrind is:

==2570== 265 (4 direct, 261 indirect) bytes in 1 blocks are definitely lost in loss record 87 of 114
==2570== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2570== by 0x40772A2: fgHintPresent (freeglut_init.c:217)
==2570== by 0x4077959: glutInit (freeglut_init.c:291)
==2570== by 0x804880E: main (main.c:15)
==2570==
==2570== 265 (4 direct, 261 indirect) bytes in 1 blocks are definitely lost in loss record 88 of 114
==2570== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2570== by 0x40772A2: fgHintPresent (freeglut_init.c:217)
==2570== by 0x407798A: glutInit (freeglut_init.c:299)
==2570== by 0x804880E: main (main.c:15)
==2570==
==2570== 4,028 (2,072 direct, 1,956 indirect) bytes in 1 blocks are definitely lost in loss record 110 of 114
==2570== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2570== by 0x42A19CD: ??? (in /usr/lib/mesa/libGL.so.1.2)
==2570== by 0x42A1E01: ??? (in /usr/lib/mesa/libGL.so.1.2)
==2570== by 0x427D285: ??? (in /usr/lib/mesa/libGL.so.1.2)
==2570== by 0x427A26E: glXGetFBConfigs (in /usr/lib/mesa/libGL.so.1.2)
==2570== by 0x427C402: glXChooseFBConfig (in /usr/lib/mesa/libGL.so.1.2)
==2570== by 0x407F7F3: fgChooseFBConfig (freeglut_window.c:205)
==2570== by 0x407F9FF: fgOpenWindow (freeglut_window.c:768)
==2570== by 0x407DC3C: fgCreateWindow (freeglut_structure.c:106)
==2570== by 0x407F1A4: glutCreateWindow (freeglut_window.c:1183)
==2570== by 0x804881A: main (main.c:16)
==2570==
==2570== LEAK SUMMARY:
==2570== definitely lost: 2,080 bytes in 3 blocks
==2570== indirectly lost: 2,478 bytes in 14 blocks
==2570== possibly lost: 0 bytes in 0 blocks
==2570== still reachable: 57,542 bytes in 557 blocks
==2570== suppressed: 0 bytes in 0 blocks
==2570== Reachable blocks (those to which a pointer was found) are not shown.
==2570== To see them, rerun with: --leak-check=full --show-reachable=yes
==2570==
==2570== For counts of detected and suppressed errors, rerun with: -v
==2570== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 71 from 13)

is there a way to reduce the errors further?

thanks
clemens

Discussion

  • Diederick C. Niehorster

    Dear clemensx,

    Thank you for the report. Could you take it to the freeglut mailing list? https://lists.sourceforge.net/lists/listinfo/freeglut-developer

    Think we'll be able to help you better there (I dont have access to a Linux system btw, plase add linux tot he title of your post).

    Thanks!
    Dee

     
  • clemens

    clemens - 2011-12-13

    Hi Dee,
    OK, I've sent it to freeglut-developer@lists.sourceforge.net after registering. I'm also running on Win, but testing is often done under Ubuntu.
    clemens

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks