SDL memory leak
所以我试图在SDL上做一些事情,但是在第一个程序上我有内存空间(是否存在IDK泄漏),所以有一些代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <stdio.h> #include <SDL.h> #include <SDL_image.h> #include <SDL_ttf.h> #define SCREENSIZEX 180 #define SCREENSIZEY 300 SDL_Window* mainwind = NULL; SDL_Renderer* rend = NULL; TTF_Font* Usefont = NULL; int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_EVERYTHING); Uint32 windowflags; windowflags = SDL_WINDOW_SHOWN; mainwind = SDL_CreateWindow("FooBar", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREENSIZEX, SCREENSIZEY, windowflags); rend = SDL_CreateRenderer(mainwind, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawColor(rend, 255, 255, 255, 255); int imgFlags = IMG_INIT_PNG; IMG_Init(imgFlags); TTF_Init(); Usefont = TTF_OpenFont("DOTMBold.TTF",90); SDL_Surface* TextSurf = NULL; SDL_Texture* TextTexture = NULL; SDL_Color UsingColor; UsingColor.r=0; UsingColor.g=255; UsingColor.b=255; UsingColor.a=100; bool exit = false; char Text[500]; int counter = 0; SDL_Event evneet; while(!exit) { SDL_PollEvent(&evneet); SDL_RenderClear(rend); counter++; TextSurf = TTF_RenderUTF8_Blended(Usefont, Text, UsingColor); TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf); SDL_FreeSurface(TextSurf); TextSurf = NULL; SDL_RenderCopy(rend, TextTexture, NULL, NULL); TextTexture = NULL; SDL_DestroyTexture(TextTexture); SDL_RenderPresent(rend); } SDL_FreeSurface(TextSurf); TextSurf = NULL; SDL_DestroyTexture(TextTexture); SDL_DestroyRenderer(rend); SDL_DestroyWindow(mainwind); SDL_Quit(); return 0; } |
问题:
一些截图
Idk如何解决此问题,并尝试进行许多释放和内存操作。
该程序仅执行一项任务。 仅计数帧(在代码中仅显示0)
这是我尝试进行渲染的第三次尝试,但我总是得到相同的结果。
请帮忙!
这看起来很可疑:
1 2 3 4 5 6 7 8 9 | while(!exit) { ... TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf); ... TextTexture = NULL; // A SDL_DestroyTexture(TextTexture); // B ... } |
答案很奇怪。 谢谢Ctx,这就是重新渲染表面和纹理的过程的第一部分。 对于我来说,最大的错误是当我需要传递" nullptr"时将纹理清空。
1 2 3 | SDL_FreeSurface(AlreadyDrawedAndUsedOnSurface); AlreadyDrawedAndUsedOnSurface = NULL; //BAD! AlreadyDrawedAndUsedOnSurface = nullptr; //good) |
对我来说很奇怪,但是有效!