gpt4 book ai didi

c++ Allegro 5 - 内存泄漏(valgrind)

转载 作者:行者123 更新时间:2023-11-30 14:42:07 25 4
gpt4 key购买 nike

我尝试使用 allegro.cc 库编写简单的游戏,一开始我发现 valgrind 发现了内存泄漏。谁能告诉我我做错了什么?我不相信 allegro 有内存泄漏。代码:

#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 个 block 中丢失了 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)

关于c++ Allegro 5 - 内存泄漏(valgrind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54581121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com