c segmentation-fault sdl sdl-2

Textura de carga de falla de segmentación con SDL2



segmentation-fault sdl-2 (3)

Acabo de terminar un clon de tetris usando SDL 1.2, ahora estoy tratando de hacer una mejor versión usando SDL2. Pero tengo un fallo de segmentación y no sé por qué.

Aquí está el informe 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.

Y las funciones:

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; }

¿Qué está causando la falla de segmentación aquí?

Actualización: cambiar el renderizador a SDL_RENDERER_SOFTWARE resuelve el problema, pero me gustaría usar SDL_RENDERER_ACCELERATED

No creo que el problema sea el controlador, ya que estoy usando SDL_RENDERER_ACCELERATED éxito en otro programa.


Asegúrese de usar SDL_CreateRender() para poner -1 como su segundo argumento, entonces SDL auto elige una tarjeta de video válida para usar.

Si el problema persiste, actualice los controladores de la tarjeta de video (reinicie la computadora después).

Comenta si todavía persiste


No parece que liberas la fuente en tu código. Tratar:

TTF_CloseFont(TTF_Font *font);

después de que termine de usar la fuente o causará un error de segmentación después de unos cientos de cuadros.


No creo que el problema sea el controlador, ya que estoy usando SDL_RENDERER_ACCELERATED con éxito en otro programa.

, es probable que haya un problema en su programa y no en la biblioteca. Al ver el informe de errores de Valgrind, su programa parece tener un gran desorden y la memoria se corrompe. Esto es un poco difícil de encontrar al buscar su código retazo. Puede haber tres razones probables de su problema:

  1. Heap Memory Overflow / Reading Después de liberar la memoria.
  2. Escenario de desbordamiento de pila.
  3. Problema de sincronización en hilos que resulta en algún tipo de corrupción de memoria.

Todos estos problemas son bastante difíciles de identificar y no hay una forma de corte para encontrar estos errores en los programas grandes, excepto en la depuración. Puede recomendar mi publicación anterior en Valgrind / GDB y Helgrind .

https://.com/a/22658693/2724703

https://.com/a/22617989/2724703