我尝试用allegro.cc库编写简单的游戏,最初我有valgrind发现的内存泄漏。谁能告诉我,我错了什么?我不相信快板有内存泄漏。代码:
#include <stdio.h>
#include <stdlib.h>
#include <allegro5/allegro.h>
int main(int argc, char** argv)
{
printf("start\n");
if (!al_init())
exit(EXIT_FAILURE);
ALLEGRO_DISPLAY *display = al_create_display(100, 100);
if (display == NULL)
exit(EXIT_FAILURE);
al_destroy_display(display);
al_uninstall_system();
return EXIT_SUCCESS;
}
我在Linux Ubuntu 16.04上运行它。
编译代码的命令:gcc -Wall test.c -o test.o $(pkg-config --libs allegro-5)
命令运行valgrind:valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./test.o
Valgrind日志:
LEAK SUMMARY:
==28629== definitely lost: 88 bytes in 2 blocks
==28629== indirectly lost: 2,668 bytes in 10 blocks
==28629== possibly lost: 0 bytes in 0 blocks
==28629== still reachable: 121,860 bytes in 675 blocks
==28629== suppressed: 0 bytes in 0 blocks
==28629== Reachable blocks (those to which a pointer was found) are not shown.
==28629== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==28629==
==28629== For counts of detected and suppressed errors, rerun with: -v
==28629== ERROR SUMMARY: 388 errors from 325 contexts (suppressed: 0 from 0)
valgrind报告了2个块中160个丢失的字节。一个是32个字节,另一个是128个直接丢失。这是在使用Allegro 5.2.5的Ubuntu 18.04上。你可以在下面看到它们:
据报道记忆被allegro泄露,但可能一无所获。我会调查一下。您自己的代码没有任何问题,所以不要担心内存泄漏。
==23579== 32 bytes in 1 blocks are definitely lost in loss record 386 of 1,647
==23579== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23579== by 0x11B596A1: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
==23579== by 0x67DBA07: glXCreateWindow (in /usr/lib/x86_64-linux-gnu/libGLX.so.0.0.0)
==23579== by 0x4F49B18: _al_xglx_config_create_context (xglx_config.c:572)
==23579== by 0x4F4328F: xdpy_create_display_locked (xdisplay.c:464)
==23579== by 0x4F43A57: xdpy_create_display (xdisplay.c:614)
==23579== by 0x4EB5D1D: al_create_display (display.c:53)
==23579== by 0x108978: main (gtktest.c:17)
==23579== 1,960 (128 direct, 1,832 indirect) bytes in 1 blocks are definitely lost in loss record 1,626 of 1,647
==23579== at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23579== by 0x11B7C93A: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
==23579== by 0x11B59171: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
==23579== by 0x11B5977C: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
==23579== by 0x67DBA07: glXCreateWindow (in /usr/lib/x86_64-linux-gnu/libGLX.so.0.0.0)
==23579== by 0x4F49B18: _al_xglx_config_create_context (xglx_config.c:572)
==23579== by 0x4F4328F: xdpy_create_display_locked (xdisplay.c:464)
==23579== by 0x4F43A57: xdpy_create_display (xdisplay.c:614)
==23579== by 0x4EB5D1D: al_create_display (display.c:53)
==23579== by 0x108978: main (gtktest.c:17)