基本 SDL 内存泄漏 Valgrind

问题描述 投票:0回答:1

这里我正在 SDL2 中制作一个基本程序,我在其中打开一个窗口。 但是当我在程序上执行 valgrind 时,它告诉我有一堆内存泄漏,而且我似乎无法追踪它们。我正在制作一个游戏,但过了一段时间我注意到这些内存泄漏,所以我将代码减少到最低限度,并且这种情况不断发生。真正让我担心的是数量,如果只是一个小的内存泄漏,我会因为库而判断它是正常的。我真的可以利用你的专业知识,谢谢。

这是我的主要内容:

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

int main()
{   
   if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    // Create a window
    SDL_Window* window = SDL_CreateWindow("SDL Loop Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
    if (window == nullptr) {
        std::cerr << "SDL could not create the window! SDL_Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    // Create a renderer
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == nullptr) {
        std::cerr << "SDL could not create the renderer! SDL_Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    // Main loop flag
    bool quit = false;

    // Event handler
    SDL_Event e;

    // Main loop
    while (!quit) {
        // Handle events
        while (SDL_PollEvent(&e) != 0) {
            if (e.type == SDL_QUIT) {
                quit = true;
            }
        }

        // Clear the renderer
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        // Draw your content here (you can render textures, shapes, etc.)

        // Present the renderer
        SDL_RenderPresent(renderer);
    }

    // Cleanup and quit
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

这是 valgrind 总结:

HEAP SUMMARY:
==12842==     in use at exit: 446,667 bytes in 4,452 blocks
==12842==   total heap usage: 154,878 allocs, 150,426 frees, 77,040,329 bytes allocated
==12842== 
==12842== LEAK SUMMARY:
==12842==    definitely lost: 66,720 bytes in 8 blocks
==12842==    indirectly lost: 71,685 bytes in 962 blocks
==12842==      possibly lost: 0 bytes in 0 blocks
==12842==    still reachable: 308,262 bytes in 3,482 blocks
==12842==         suppressed: 0 bytes in 0 blocks
==12842== Rerun with --leak-check=full to see details of leaked memory
==12842== 
==12842== Use --track-origins=yes to see where uninitialised values come from
==12842== For lists of detected and suppressed errors, rerun with: -s
==12842== ERROR SUMMARY: 10000000 errors from 397 contexts (suppressed: 0 from 0)

And here is a valgrind report extract:

Invalid read of size 8
==12842==    at 0x4852934: memmove (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==12842==    by 0x681EED1: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==12842==    by 0x725C6CA: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==12842==    by 0x67BE36D: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==12842==    by 0x6231991: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
==12842==    by 0x623571E: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
==12842==    by 0x4950FD6: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.18.2)
==12842==    by 0x10A93B: main (main.cpp:59)
==12842==  Address 0x7f6bccd07020 is not stack'd, malloc'd or (recently) free'd
==12842== 
==12842== Invalid write of size 2
==12842==    at 0x484FF63: memcpy@GLIBC_2.2.5 (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==12842==    by 0x12EE1B55: ??? (in /usr/lib/wsl/drivers/u0391195.inf_amd64_09ef84849323988b/B391209/amdxc64.so)
==12842==    by 0x12D8986A: ??? (in /usr/lib/wsl/drivers/u0391195.inf_amd64_09ef84849323988b/B391209/amdxc64.so)
==12842==    by 0x12E606B6: ??? (in /usr/lib/wsl/drivers/u0391195.inf_amd64_09ef84849323988b/B391209/amdxc64.so)
==12842==    by 0x12E76E73: ??? (in /usr/lib/wsl/drivers/u0391195.inf_amd64_09ef84849323988b/B391209/amdxc64.so)
==12842==    by 0x12E8E052: ??? (in /usr/lib/wsl/drivers/u0391195.inf_amd64_09ef84849323988b/B391209/amdxc64.so)
==12842==    by 0x12A93A1A: ??? (in /usr/lib/wsl/drivers/u0391195.inf_amd64_09ef84849323988b/B391209/amdxc64.so)
==12842==    by 0x11BA4E11: ??? (in /usr/lib/wsl/lib/libd3d12core.so)`
==12842==    by 0x11BA4C41: ??? (in /usr/lib/wsl/lib/libd3d12core.so)
==12842==    by 0x7267189: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==12842==    by 0x72672CF: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==12842==    by 0x6D5A362: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==12842==  Address 0x7f6bccbf72e4 is not stack'd, malloc'd or (recently) free'd
c++ sdl valgrind sdl-2
1个回答
0
投票

发现我正在使用wsl,而sdl和linux有问题。所以我安装了 XLaunch,有关更多详细信息,请参阅本指南 youtu.be/sC3M6C8IpoE。

© www.soinside.com 2019 - 2024. All rights reserved.