SDL 内存泄漏
SDL memory leak
所以我尝试在 SDL 上做一些事情,但是在第一个程序中我有内存泄漏(idk 泄漏与否)所以有一些代码:
#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;
}
问题:
some screenshots
不知道如何解决这个问题,并尝试进行大量的释放和内存操作。
这个程序只做一个任务。只计算帧数(在代码中只显示 0)
这是我第三次尝试渲染,我总是得到相同的结果。
请帮忙!
这看起来很可疑:
while(!exit)
{
...
TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf);
...
TextTexture = NULL; // A
SDL_DestroyTexture(TextTexture); // B
...
}
SDL_DestroyTexture()
在这里没有得到一个有效的句柄,但是传递了一个 NULL
-指针。您必须交换 A 和 B 行,这样才能正确释放 Texture。
答案很奇怪。感谢 Ctx,这是重新渲染表面和纹理的大混乱的第一部分。至于我最大的错误是当我需要传递 "nullptr".
时 NULLing texture
SDL_FreeSurface(AlreadyDrawedAndUsedOnSurface);
AlreadyDrawedAndUsedOnSurface = NULL; //BAD!
AlreadyDrawedAndUsedOnSurface = nullptr; //good)
对我来说很奇怪,但它有效!
所以我尝试在 SDL 上做一些事情,但是在第一个程序中我有内存泄漏(idk 泄漏与否)所以有一些代码:
#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;
}
问题: some screenshots
不知道如何解决这个问题,并尝试进行大量的释放和内存操作。 这个程序只做一个任务。只计算帧数(在代码中只显示 0) 这是我第三次尝试渲染,我总是得到相同的结果。 请帮忙!
这看起来很可疑:
while(!exit)
{
...
TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf);
...
TextTexture = NULL; // A
SDL_DestroyTexture(TextTexture); // B
...
}
SDL_DestroyTexture()
在这里没有得到一个有效的句柄,但是传递了一个 NULL
-指针。您必须交换 A 和 B 行,这样才能正确释放 Texture。
答案很奇怪。感谢 Ctx,这是重新渲染表面和纹理的大混乱的第一部分。至于我最大的错误是当我需要传递 "nullptr".
时 NULLing textureSDL_FreeSurface(AlreadyDrawedAndUsedOnSurface);
AlreadyDrawedAndUsedOnSurface = NULL; //BAD!
AlreadyDrawedAndUsedOnSurface = nullptr; //good)
对我来说很奇怪,但它有效!