2

可能重复:
为什么 valgrind 说基本 SDL 程序正在泄漏内存?

所以我在创建一个小型 OpenGL 应用程序时经常使用 SDL。我最近开始对检查我的内存情况感兴趣(我已经尽可能地注意确保没有内存泄漏等)。但是后来我在我的程序中创建了 SDL 驱动程序,结果一切都变了。我已经隔离了代码,不知道我是否做错了什么,如果它是 SDL,或者它可能是 SDL 正在使用的库(参见 Valgrind 结果)。

#include <SDL.h>

int main() {
    SDL_Surface* Screen;
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        return 1;
    }
    Screen = SDL_SetVideoMode(1280, 1024, 32, SDL_OPENGL);
    SDL_FreeSurface(Screen);
    SDL_Quit();
    return 0;
}

现在我不能完全隔离超过 1 个部分,所以我将在此处发布 Valgrind 的结果,用于 init 本身,然后是整个程序。

if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
    return 1;
}

==7485== LEAK SUMMARY:
==7485==    definitely lost: 16 bytes in 1 blocks
==7485==    indirectly lost: 176 bytes in 4 blocks
==7485==      possibly lost: 9,155 bytes in 14 blocks
==7485==    still reachable: 654,484 bytes in 1,484 blocks

注意:在上述代码之后使用 SDL_Quit() 会导致进一步的内存泄漏,因此这不是解决方法(很遗憾)。现在对于整个程序(作为初始源发布)(在显示以下内容之前,第一个错误重复了几次):

==7515== Invalid write of size 1
==7515==    at 0x4C29910: memcpy (mc_replace_strmem.c:497)
==7515==    by 0xB9BE0CF: ??? (in /usr/lib/fglrx/dri/fglrx_dri.so)
==7515==  Address 0x7f42bca30fff is not stack'd, malloc'd or (recently) free'd
==7515== 
==7515== More than 10000000 total errors detected.  I'm not reporting any more.
==7515== Final error counts will be inaccurate.  Go fix your program!
==7515== Rerun with --error-limit=no to disable this cutoff.  Note
==7515== that errors may occur in your program without prior warning from
==7515== Valgrind, because errors are no longer being displayed.
==7515== 
==7515== HEAP SUMMARY:
==7515==     in use at exit: 4,539,966 bytes in 11,312 blocks
==7515==   total heap usage: 49,855 allocs, 38,543 frees, 40,485,815 bytes allocated
==7515== 
==7515== LEAK SUMMARY:
==7515==    definitely lost: 79,981 bytes in 126 blocks
==7515==    indirectly lost: 30,480 bytes in 54 blocks
==7515==      possibly lost: 3,374,770 bytes in 9,289 blocks
==7515==    still reachable: 1,054,735 bytes in 1,843 blocks
==7515==         suppressed: 0 bytes in 0 blocks
==7515== Rerun with --leak-check=full to see details of leaked memory

如果重要的话,我正在使用 Ubuntu 10.10。

第二注;我正在使用 Valgrind (如果这是问题?):

valgrind ./main

第三注;fglrx 是 Ubuntu ATI 驱动程序。

4

0 回答 0