We are developing Tetris as Computer Scientist High School project, but we are struggling with some kind of segmentation fault. To help my studente I developed a layer that semplify the use of SDL2, I am calling it EasySDL (you can look at the code on GitHub GitHub - kismet/tetris-2024).
I am trying to figure out what I haven’t understood of SDL2. The first candidate for segmentation fault is my drawText(…) function where SDL_Rect object was declared on stack and no SDL_RenderPresent was invoked with the function (see the code at the bottom).
Considering SDL_RenderCopy uses a SDL_Rect* , does it mean that I have to call the SDL_RenderPresent after each SDL_RenderCopy or within the scope of the variable?
Storing the SDL_Rect in memory heap is also an option?
The src and dst rects used by SDL_RenderCopy do not need to persist in memory after SDL_RenderCopy has returned, so stack memory like you have here is fine; the copied texture data now exists on the render target (which is the window by default). Have you tried to locate the memory error using print statements or a debugger?
Is the segfault happening when you try to close the program, on a certain scene (game, menu, pause, credits) or is it happening any time you try to call drawText()?
P.S. Thank you for sharing your source code, I perused it a bit but I’ll have to find more time to dig deeper. Sadly I can’t compile at the moment because I’m getting warnings that my cmake program is too old (Ubuntu apt-installed). I’ll have to install cmake from source before I can test your code on my system.
Thank you for the effort. I am trying to fight the “segmentation fault” with all the tools:
printing out messages
break point
reducing the codebase that hang up the code
Regarding the codebase, we are using CLion and Code::Block on Win64 as the ONLY working enviroment, and the configuration requries the user of the code to copy the assets directory to the build code as long the DLL.
Ciao,
Stefano
P.S.: I will come out with more messages on the topic (too tired last night)
(For some reason I had to also change the includes for SDL_ttf from #include <SDL_ttf.h> to #include <SDL2/SDL_ttf.h> to make it work.)
@kismet Running the program through valgrind gives the following message:
==2537== Invalid write of size 1
==2537== at 0x483BDF6: strcpy (vg_replace_strmem.c:511)
==2537== by 0x10AE20: loadFont(char*) (easy_sdl.cpp:205)
==2537== by 0x10AB96: loadAsset(char*) (easy_sdl.cpp:154)
==2537== by 0x10A436: loadingAssets() (easy_menu.cpp:25)
==2537== by 0x10A617: main (easy_menu.cpp:117)
==2537== Address 0x1ab132e4 is 0 bytes after a block of size 20 alloc'd
==2537== at 0x483877F: malloc (vg_replace_malloc.c:307)
==2537== by 0x10ADFE: loadFont(char*) (easy_sdl.cpp:204)
==2537== by 0x10AB96: loadAsset(char*) (easy_sdl.cpp:154)
==2537== by 0x10A436: loadingAssets() (easy_menu.cpp:25)
==2537== by 0x10A617: main (easy_menu.cpp:117)
Valgrind is a great tool, but I don’t think it’s available on Windows. “Sanitizers”, that come with many modern compilers nowadays, provide similar functionality.
I noticed that a similar issue got fixed in the SDL codebase just a few hours ago so you are apparently not alone in making this sort of mistake.
My students and I would like to thank you all for the amazing support! We had the chance to present our work at our science festival and it was a success!
People didn’t belived that it was made by 16 years old student