【问题标题】:SDL 2 program uses 1.4 GB of memory?SDL 2 程序使用 1.4 GB 内存?
【发布时间】:2014-08-08 22:29:49
【问题描述】:

好的,所以我一直在制作这个弹跳 DVD 徽标的小东西,我正慢慢地跑到它上面,占用越来越多的内存。最终它占用了高达 1.4 GB 的空间,然后速度变慢并崩溃。这是代码,它有什么问题导致它这样做?

#include <iostream>
#include <stdlib.h>
#include <time.h>

#include <SDL2/SDL.h>
#include <SDL2_ttf/SDL_ttf.h>
#include <SDL2_image/SDL_image.h>
// This sets ups the display.
SDL_Window* window = SDL_CreateWindow("DVD Thingy", 100, 100,
                                        800, 600, SDL_WINDOW_SHOWN
                                        | SDL_RENDERER_ACCELERATED
                                        | SDL_RENDERER_PRESENTVSYNC);
SDL_Renderer* screen = SDL_CreateRenderer(window, -1, 0);

void drawText(char text[], int origX, int origY, SDL_Renderer* ren, TTF_Font* font, SDL_Color color) {
    SDL_Surface* surfaceMessage = TTF_RenderText_Blended(font, text, color);
    SDL_Texture* Message = SDL_CreateTextureFromSurface(ren, surfaceMessage);
    int w = surfaceMessage->w;
    int h = surfaceMessage->h;
    SDL_Rect messageRect = {origX, origY, w, h};
    SDL_RenderCopy(ren, Message, NULL, &messageRect);

    SDL_DestroyTexture(Message);
}

int main() {
    // This initializes the font class.
    srand(time(NULL));
   TTF_Init();
   int skyboxColor = 240;
   bool done = false;
   int dirX = 1, dirY = 1;
   TTF_Font* font = TTF_OpenFont("./Impact.ttf", 18);
   TTF_SetFontOutline(font, 1);
   int dvdX = rand() % 800, dvdY = rand() % 600-20;
   SDL_Color white = {255, 255, 255};
   SDL_Event event;
   while (!done) {
        while (SDL_PollEvent(&event)) {
            switch(event.type) {
                case SDL_QUIT:
                    SDL_Quit();
                    return 0;
                default:
                    break;
            }
        }
        dvdX += dirX;
        dvdY += dirY;
        if (dvdX > 770) {
            dirX = -1;
        }
        if (dvdX < 0) {
            dirX = 1;
        }
        if (dvdY < -3) {
            dirY = 1;
        }
        if (dvdY > 580) {
            dirY = -1;
        }
        SDL_SetRenderDrawColor( screen, 0, 0, 0, 255);
        SDL_RenderClear(screen);
        drawText("DVD", dvdX, dvdY, screen, font, white);
        SDL_RenderPresent(screen);
        SDL_Delay (1/1000 * 60);
    }
    return 0;
}

【问题讨论】:

  • 听起来像是内存泄漏。如果你在 Linux 上,运行 Valgrind 或类似的,它应该会告诉你内存泄漏在哪里(如果你有的话)。
  • The caller (you!) is responsible for freeing any returned surface. - libsdl.org/projects/docs/SDL_ttf/SDL_ttf_44.html
  • @dari 解决了这个问题!谢谢!
  • SDL_Delay (1/1000 * 60); 将始终传递 0。1000 / desiredFPS 更可能是您想要的,尽管它没有考虑自上一帧以来的任何处理时间。

标签: c++ memory sdl sdl-2


【解决方案1】:

drawText() 函数中,您似乎正在通过调用 TTF_RenderText_Blended() 创建一个新的 SDL_Surface。

您必须确保在完成此表面后释放它,这似乎是在创建它的函数的末尾。您已经破坏了从表面创建的纹理,因此您需要添加的所有内容之后是一行:

SDL_DestroyTexture(Message);
SDL_FreeSurface(surfaceMessage);   <- Free the surface

由于在主 while 循环中不断调用 drawText(),因此它使用 SDL_Surfaces 使内存膨胀。

还有一点,因为您似乎没有更改“DVD”中的文本,您可以创建一次纹理,然后在需要的地方绘制它。这比创建、绘制然后销毁每一个单独的戏剧要高效得多。

【讨论】:

    猜你喜欢
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    相关资源
    最近更新 更多