gpt4 book ai didi

c - 使用 SDL2 的段错误加载纹理

转载 作者:行者123 更新时间:2023-12-01 13:26:11 28 4
gpt4 key购买 nike

我刚刚使用 SDL 1.2 完成了一个俄罗斯方 block 克隆,现在我正在尝试使用 SDL2 制作一个更好的版本。但是我遇到了段错误,我不知道为什么。

这是 valgrind 报告:

==9471== Memcheck, a memory error detector
==9471== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==9471== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==9471== Command: ./tetris
==9471==
==9471== Thread 2:
==9471== Invalid read of size 8
==9471== at 0xB4B57A9: ??? (in /usr/lib/x86_64-linux-gnu/nvidia/current/libGL.so.319.82)
==9471== by 0x4E8094E: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.1.0)
==9471== by 0x4E799EA: SDL_CreateTexture (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.1.0)
==9471== by 0x4E79C6D: SDL_CreateTextureFromSurface (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.1.0)
==9471== by 0x401176: load_texture.3137.2439
==9471== by 0x401194: video_load_image.2436
==9471== by 0x4011B6: block_image_load_all.2429
==9471== by 0x4017BD: run_game_logic.2384
==9471== by 0x541B061: start_thread (pthread_create.c:312)
==9471== by 0x5715A3C: clone (clone.S:111)
==9471== Address 0x8c0 is not stack'd, malloc'd or (recently) free'd
==9471==
==9471==
==9471== Process terminating with default action of signal 11 (SIGSEGV)
==9471== Access not within mapped region at address 0x8C0
==9471== at 0xB4B57A9: ??? (in /usr/lib/x86_64-linux-gnu/nvidia/current/libGL.so.319.82)
==9471== by 0x4E8094E: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.1.0)
==9471== by 0x4E799EA: SDL_CreateTexture (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.1.0)
==9471== by 0x4E79C6D: SDL_CreateTextureFromSurface (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.1.0)
==9471== by 0x401176: load_texture.3137.2439
==9471== by 0x401194: video_load_image.2436
==9471== by 0x4011B6: block_image_load_all.2429
==9471== by 0x4017BD: run_game_logic.2384
==9471== by 0x541B061: start_thread (pthread_create.c:312)
==9471== by 0x5715A3C: clone (clone.S:111)
==9471== If you believe this happened as a result of a stack
==9471== overflow in your program's main thread (unlikely but
==9471== possible), you can try to increase the size of the
==9471== main thread stack using the --main-stacksize= flag.
==9471== The main thread stack size used in this run was 8388608.

还有函数:

SDL_Texture *video_load_image(const char *file)
{
return load_texture(file);
}

static SDL_Texture *load_texture(const char *path)
{
SDL_Surface *surface;
SDL_Texture *texture;

if((surface = SDL_LoadBMP(path)) == NULL){
puts("invalid path");
return NULL;
}

texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);

return texture;
}

char *image_path[BLOCK_COUNT] = {
"img/block_dark_cyan32.bmp", "img/block_dark_red32.bmp",
"img/block_dark_brown32.bmp", "img/block_dark_magenta32.bmp",
"img/block_dark_gray32.bmp", "img/block_dark_green32.bmp",
"img/block_dark_blue32.bmp",
"img/block_wall32.bmp", "img/block_empty32.bmp"
};

SDL_Texture *textures[BLOCK_COUNT];

int block_image_load_all(void)
{
for(int i = 0; i < BLOCK_COUNT; ++i){
if((textures[i] = video_load_image(image_path[i])) == NULL){
while(i > 0)
video_free_image(textures[--i]);

return ERROR;
}
}

return SUCCESS;
}

是什么导致了这里的段错误?

更新:将渲染器切换到 SDL_RENDERER_SOFTWARE 可以解决问题,但我想使用 SDL_RENDERER_ACCELERATED

我不认为问题出在驱动程序上,因为我在另一个程序中成功地使用了 SDL_RENDERER_ACCELERATED

最佳答案

确保在使用 SDL_CreateRender() 时将 -1 作为第二个参数,以便 SDL 自动选择要使用的有效视频卡。

如果问题仍然存在,请更新您的视频卡驱动程序(之后重新启动计算机)。

如果它仍然存在,请评论

关于c - 使用 SDL2 的段错误加载纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774751/

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