ivan rojas
ivan rojas

Reputation: 21

Why valgrind report my memory as definitely lost 12 or 24 bytes using GLUT and PORTAUDIO

I wrote some code with portaudio and tested it with Valgrind. As a result, I got "0 bytes definitely lost". But, when I mixed it with the glut (freeglut) library to include graphics, I got "24 bytes definitely lost".

In the code below I simply added glutCreateWindow( "OpenGL" ) and then I got the memory leak in Valgrind's report. I'm using glew, freeglut, portaudio, sndfile libraries. Does any know what is happening here?

#include "util.h"
#include "sound.h"
#include <unistd.h>

/*Funcion main para creacion de hilos de reproduccion*/
int main(int argc, char* args[]){
    sound::loadStream("../pacman.wav");// this function do Pa_Initiliaze()/Pa_OpenDefaultStream()/ 
                                       //Pa_StartStream()/
    sound::loadStream("../ya_te.wav");
     glutInit( &argc, args );
    //Create OpenGL 2.1 context
    glutInitContextVersion( 2, 1 );
    //Create Double Buffered and RGBA Window
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
    glutInitWindowSize( 1920, 1080 ); //size of window
    glutCreateWindow( "OpenGL" ); // leak memory start at this line
    sound::changeState(0,PLAY);  //this fucntion change pacman.wav state to PLAY in portaudio 
                                 //callback
    sound::changeState(1,PLAY); // this function change ya_te.wav state to PLAY in portaudio 
                                //callback
    sleep(1); // sleep for 1 second
    sound::soundTerminate(); // this function do Pa_closeStream , Pa_terminate();
    glutDestroyWindow(glutGetWindow());
    return 0;
}

Upvotes: 2

Views: 233

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51874

You aren't 'keeping track' of the window created in your call to glutCreateWindow, which means that (as Valgrind sees it) you can never properly free the memory used by that window.

You should store the return value of the call, like this:

int winID = glutCreateWindow( "OpenGL" );

and then use the saved window identifier in your subsequent call to destroy it:

glutDestroyWindow(winID);

NOTE: Even though your use of glutGetWindow() will (most likely) return the same value, Valgrind does not (or cannot) know this.

Upvotes: 1

Related Questions