gpt4 book ai didi

c - SDL 内存泄漏

转载 作者:行者123 更新时间:2023-12-02 08:06:17 25 4
gpt4 key购买 nike

所以我尝试在 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 行,以便可以正确释放纹理。

关于c - SDL 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51126918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com