gpt4 book ai didi

c - SDL2 DestroyRenderer SEGFAULT

转载 作者:行者123 更新时间:2023-11-30 16:32:55 24 4
gpt4 key购买 nike

我想出了一个我的程序在调用 SDL_DestroyRenderer(SDL_Renderer* renderer); 时抛出 SEGFAULT 的最小示例。

int Setup(t_game* game)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "Error Init SDL");
return 0;
}

game->window = SDL_CreateWindow("Client",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_X,
SCREEN_Y,
SDL_WINDOW_SHOWN);

game->renderer = SDL_CreateRenderer(game->window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(game->renderer, 255, 0, 0, 255);

return 1;
}

void Cleanup(t_game* game)
{
SDL_DestroyRenderer(game->renderer);

SDL_DestroyWindow(game->window);
SDL_Quit();
}

void Loop(t_game* game)
{
SDL_Event event;
int loop = 1;
while (loop)
{
SDL_RenderClear(game->renderer);
SDL_RenderPresent(game->renderer);
loop = EventHandler(&event, game);
}
}

从 main 中调用函数,如下所示:

int main()
{
t_game game;
Setup(&game);
Loop(&game);
Cleanup(&game);
return 0;
}

游戏结构声明如下:

typedef struct s_game
{
SDL_Renderer* renderer;
SDL_Window* window;
} t_game;

在清理过程中,我收到一个SEGFAULT,表示“无效免费”

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ae1f9c in ?? () from /usr/lib/libSDL2-2.0.so.0
(gdb) bt
#0 0x00007ffff7ae1f9c in ?? () from /usr/lib/libSDL2-2.0.so.0
#1 0x00005555555554d0 in Cleanup (game=0x7fffffffdd90)
#2 0x00005555555555e9 in main (argc=1, argv=0x7fffffffdea8)

但是在 gdb 中,在所有 3 个函数中设置断点后,我始终具有相同的渲染器指针。

更新 1:如果我将所有三个函数放在一起,我就不会再出现错误了。更新2:

int EventHandler(SDL_Event* event, t_game* game)
{
while (SDL_PollEvent(event))
{
switch(event->type)
{
case SDL_QUIT:
return 0;
break;
case SDL_KEYUP:
switch (event->key.keysym.sym)
{
case SDLK_ESCAPE:
return 0;
}
}
}
return 1;
}

最佳答案

似乎大页面以某种方式干扰了 SDL2,因为禁用它可以解决无效的空闲错误。

关于c - SDL2 DestroyRenderer SEGFAULT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49927590/

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