【问题标题】:Why valgrind report my memory as definitely lost 12 or 24 bytes using GLUT and PORTAUDIO为什么 valgrind 使用 GLUT 和 PORTAUDIO 报告我的内存肯定丢失了 12 或 24 个字节
【发布时间】:2020-06-25 20:17:36
【问题描述】:

我用portaudio 编写了一些代码,并用Valgrind 对其进行了测试。结果,我得到“0字节肯定丢失”。但是,当我将它与 glut (freeglut) 库混合以包含图形时,我得到“肯定丢失了 24 个字节”。

在下面的代码中,我只是添加了glutCreateWindow( "OpenGL" ),然后我在 Valgrind 的报告中得到了内存泄漏。我正在使用 glew、freeglut、portaudio、sndfile 库。有谁知道这里发生了什么?

#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;
}

【问题讨论】:

    标签: c++ opengl valgrind freeglut portaudio


    【解决方案1】:

    您没有“跟踪”在调用 glutCreateWindow 时创建的窗口,这意味着(正如 Valgrind 所看到的)您永远无法正确释放该窗口使用的内存。

    您应该存储调用的返回值,如下所示:

    int winID = glutCreateWindow( "OpenGL" );
    

    然后在后续调用中使用保存的窗口标识符来销毁它:

    glutDestroyWindow(winID);
    

    注意:即使您使用 glutGetWindow() 将(很可能)返回相同的值,Valgrind 并不(或不能)知道这一点。

    【讨论】:

    • 虽然这是一个很好的信息性答案,但我仍然不断收到 valgrind 抱怨失去记忆。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多