为什么简单的SDL程序报告Valgrind中的内存泄漏?

时间:2014-08-16 20:44:26

标签: c++ memory-leaks sdl valgrind

这个程序:

#include <iostream>
#include <SDL2/SDL.h>

/*
 * Lesson 0: Test to make sure SDL is setup properly
 */
int main(int argc, char** argv){
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
        std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
        return 1;
    }
    SDL_Quit();

    return 0;
}

使用g++ -o main main.cpp -lSDL2; valgrind ./main编译产生以下输出:

==21646== HEAP SUMMARY:
==21646==     in use at exit: 108,320 bytes in 582 blocks
==21646==   total heap usage: 16,412 allocs, 15,830 frees, 106,043,200 bytes allocated
==21646== 
==21646== LEAK SUMMARY:
==21646==    definitely lost: 12,058 bytes in 4 blocks
==21646==    indirectly lost: 0 bytes in 0 blocks
==21646==      possibly lost: 0 bytes in 0 blocks
==21646==    still reachable: 96,262 bytes in 578 blocks
==21646==         suppressed: 0 bytes in 0 blocks

这是我应该担心的事情,如果是这样,我怎么知道我的代码中误报和实际错误之间的区别?

1 个答案:

答案 0 :(得分:0)

取自stackoverflow answer


在您的具体情况下,我将使用标志--leak-check=full --track-origins=yes --show-reachable=yes运行valgrind,查看泄漏是否真的来自SDL