2013-03-12 32 views
-2

我用SDL api编写了一个简单的蛇游戏,游戏中有一处内存泄漏,因为游戏运行大约10分钟后,它占用了50MB的内存,并且仍在扩展。我尽我所能找到了泄漏点,但找不到它。我尝试过使用Dr Memory,但是它产生的日志不是我完全可以理解的。解读DrMemory日志,内存泄露

这里的日志中的问题: https://docs.google.com/file/d/0BwXrdShTcyjENmgyR2lrbTJ1aGc/edit?usp=sharing

+3

也许你可能会切换到Linux并使用'valgrind'? – 2013-03-12 17:56:15

+1

也许有些代码..它听起来像一个循环问题 – iKlsR 2013-03-12 18:01:08

+0

根据日志你有很多内存泄漏。例如,看看这里是什么:'#5 load_image()[L:/git/snake/src/main.cpp:144] #6 load_files()[L:/ git/snake/src/main。 cpp:192]' – varnie 2013-03-12 18:09:47

回答

3

在您的Dr. Memory日志中,您可以看到您有3次错误,计数约为2899次。 的错误是:

Error # 154: 2899 
Error # 155: 2899 
Error # 369: 2898 

所以,如果你看一下错误:

Error #154: GDI USAGE ERROR: same bitmap 0x46052c24 selected into two different DC's 0x08012c43 and 0xbe012c3e 
# 0 SDL.dll!SDL_UnregisterApp  +0x3063 (0x681304c3 <SDL.dll+0x304c3>) 
# 1 SDL.dll!SDL_UpdateRect   +0x69  (0x68125d7a <SDL.dll+0x25d7a>) 
# 2 SDL.dll!SDL_Flip    +0x52  (0x68125ff3 <SDL.dll+0x25ff3>) 
# 3 draw()       [L:/git/snake/src/main.cpp:133] 
# 4 SDL_main      [L:/git/snake/src/main.cpp:92] 
# 5 console_main     [./src/main/win32/SDL_win32_main.c:315] 
# 6 [email protected]      [./src/main/win32/SDL_win32_main.c:398] 
# 7 main       [L:/git/snake/src/main.cpp:211] 
Note: @0:00:04.148 in thread 3940 

Error #155: GDI USAGE ERROR: DC 0x08012c43 that contains selected object being deleted 
# 0 system call NtGdiDeleteObjectApp 
# 1 GDI32.dll!DeleteDC       +0xb6  (0x75b1596a <GDI32.dll+0x1596a>) 
# 2 GDI32.dll!DeleteDC       +0x11  (0x75b158c5 <GDI32.dll+0x158c5>) 
# 3 SDL.dll!SDL_UnregisterApp      +0x30c9 (0x6813052a <SDL.dll+0x3052a>) 
# 4 SDL.dll!SDL_UpdateRect      +0x69  (0x68125d7a <SDL.dll+0x25d7a>) 
# 5 SDL.dll!SDL_Flip        +0x52  (0x68125ff3 <SDL.dll+0x25ff3>) 
# 6 draw()           [L:/git/snake/src/main.cpp:133] 
# 7 SDL_main          [L:/git/snake/src/main.cpp:92] 
# 8 console_main         [./src/main/win32/SDL_win32_main.c:315] 
# 9 [email protected]          [./src/main/win32/SDL_win32_main.c:398] 
#10 main           [L:/git/snake/src/main.cpp:211] 
Note: @0:00:04.149 in thread 3940 

Error #369: LEAK 60 direct bytes 0x04c09070-0x04c090ac + 0 indirect bytes 
# 0 SDL.dll!SDL_CreateRGBSurface   +0x8a  (0x681250cb <SDL.dll+0x250cb>) 
# 1 SDL_ttf.dll!TTF_RenderUNICODE_Solid +0xa6  (0x6f4c2e87 <SDL_ttf.dll+0x2e87>) 
# 2 SDL_ttf.dll!TTF_RenderText_Solid  +0x62  (0x6f4c3253 <SDL_ttf.dll+0x3253>) 
# 3 draw()        [L:/git/snake/src/main.cpp:130] 
# 4 SDL_main        [L:/git/snake/src/main.cpp:92] 
# 5 console_main       [./src/main/win32/SDL_win32_main.c:315] 
# 6 [email protected]       [./src/main/win32/SDL_win32_main.c:398] 
# 7 main         [L:/git/snake/src/main.cpp:211] 

看来,你在一些循环分配新的内存,但忘记解除分配。

对于错误#369,标记为LEAK:致电TTF_RenderText_Solid()后,您需要致电SDL_FreeSurface()

0

内存泄漏是由错误的动态分配使用(或用指针搞乱)引起的。我想你没有使用任何自定义动态分配为一个小蛇游戏,所以我想你搞砸了SDL_Surface *东西。

检查您在哪里使用SDL_Surface *。是否使用任何代码在主游戏循环中创建(分配)新的曲面

创造(分配)

SDL功能的新表面通常是SDL_LoadBMP()SDL_GetRGBSurface()SDL_SetVideoMode()。你在主游戏循环中使用它们中的任何一个吗?他们通常不应该在

或者您可能使用了任何动态分配?检查你所有的指针

+0

我消除了所有不需要的指针。剩下的唯一指针是我的蛇类指针和sdl的几个指针。我将我的SDL函数放在Lazy Foo的SDL教程上。 – Krzaku 2013-03-12 18:15:07

+0

然后,正如我所说的,检查您的SDL指针,它们不应受主循环内的“SDL_GetRGBSurface”和“SDL_LoadBMP”影响。如果它们不是,那么蛇类指针就是原因。你在主循环中使用'new'作为你的蛇类吗?如果是的话,那肯定是原因。 'new'运算符是**动态分配**。 – pampeho 2013-03-12 18:20:04

+0

在主循环的任何地方都不使用'new'。 – Krzaku 2013-03-12 18:22:44