SDL2程序在最小化时会占用大量内存

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

我只是试图用一个简单的渲染器创建一个简单的SDL2窗口。当我调试它时,它可以正常运行,内存仍然正常。但是,当我最小化它时,按下最小化按钮,然后内存突然增加,仅当我重新打开该窗口时,内存才恢复正常。

Memory spike

代码:

#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char* args[]) {
    SDL_Window* gWindow;
    SDL_Renderer* gRenderer;
    SDL_Texture* gTexture;
    SDL_Surface* temp;
    //Init
    SDL_Init(SDL_INIT_VIDEO);
    gWindow = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1366, 768, SDL_WINDOW_SHOWN);
    gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
    IMG_Init(IMG_INIT_PNG);

    //loading medias
    temp = IMG_Load("assets/sprites/700.png");
    gTexture = SDL_CreateTextureFromSurface(gRenderer, temp);
    SDL_FreeSurface(temp);

    //The main loop
    bool quit = false;
    SDL_Event e;
    while (!quit)
    {
        while (SDL_PollEvent(&e) != 0)
        {
            if (e.type == SDL_QUIT)
            {
                quit = true;
            }
        }
        SDL_RenderClear(gRenderer);
        SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
        SDL_RenderPresent(gRenderer);
    }

    //Destroy things
    SDL_DestroyTexture(gTexture);
    gTexture = NULL;   
    SDL_DestroyRenderer(gRenderer);
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;
    gRenderer = NULL;
    IMG_Quit();
    SDL_Quit();
    return 0;
}
c++ memory rendering sdl sdl-2
1个回答
0
投票

我只是运行您的代码并进行了编译。看起来不错。但是,您确实提到过,当最小化夯表增加的程序时,您能够执行此重复操作多长时间才崩溃?甚至崩溃吗?这可能是正常行为。如果您的程序崩溃,则可能是内存泄漏,这可能表明您忘记了取消分配其他指向内存对象/变量的指针。

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