Segmentation Fault hunting: SDL_Rect on heap or stack for SDL_RenderCopy?

Hi!

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?

drawText(...) {
  SDL_Texture* texture;
  texture = SDL_CreateTextureFromSurface( context.renderer, text );
  SDL_Rect dst = { x, y, text->w, text->h }
  SDL_RenderCopy( context.renderer, texture, NULL, &dst );
  SDL_DestroyTexture(texture);
  SDL_FreeSurface(text);
}

P.S.: I am going to post other question on the same “segmentation fault” topic

Thank you,
Stefano

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?

1 Like

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.

Dear @chvolow24 @GuildedDoughnut and All,

Thank you for the effort. I am trying to fight the “segmentation fault” with all the tools:

  1. printing out messages
  2. break point
  3. 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)

@GuildedDoughnut I was able to compile the program without cmake using the following command:

g++ -o run  src/*.cpp -g `sdl2-config --cflags --libs` -lSDL2_ttf -lSDL2_image -Wall

(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)

Which points us to the following code:

asset->origin = (char *) malloc(strlen(path));
strcpy(asset->origin,path);

Looks like you haven’t allocated enough space for storing the terminating null character '\0'.

1 Like

Thank you @Peter87 !

The #include issue is related to the configuration of the CMake which was not read by your g++ cmq line!

I didn’t know about valgrid (I am going to read about it!)

I am going to check fix right away! It was a very silly issue!!!

I will let you know about the progress!

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. :wink:

2 Likes

Dear All (@Peter87 @GuildedDoughnut @chvolow24)

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 :slight_smile:

This is our way to say “Thank You” :slight_smile:

BTW, The game is stable and working they are going to implement some other missing parts as “Final Term”

Ciao,
A proud teacher Stefano Lenzi

5 Likes