SDL Project Crashing After Split Second[SOLVED]

I’m making a project in C++ with SDL2 and like the title says it crashes after a split second, but if you enter the second scene before it crashes it doesn’t crash and it is fine for some reason.
I have tried everything, scouring the internet and trying for hours to fix it so I am looking for experts to help me solve this.
It started after I added the whatWillYouDoNext case of the loadImage function I made.

Video Of It Happening

Code

That “process crashed” notice your IDE is giving you is supremely unhelpful. I’m not familiar with Dolphin; is there any way you can attach a debugger to it that will trap the error and let you examine it? That should make tracking down the crash bug very, very simple.

If not, there’s always logging. Open a log file at program startup, and when various functions begin and end, have them print a message into the log file saying “function X starting/ending”. When it crashes, look at the last few entries in the log file and you’ll see what your program was doing. Then take a close look at the code you were in at that point. If necessary, add some more logging to narrow it down…

Have you tried the lazyfoo tutorials?

I used to use visual studio code but it started to get angry when I used iostream so I stopped and I use kate now that’s why it is pretty unhelpful because I compile it with g++ main.cpp -lSDL2 -lSDL2_image -o ‘Morshu Game’ but this still gives me any compiler errors.

I used GDB and I realized in the main loop it gives me a Segmentation Fault error after it reaches the renderScreen() function for the second time.

image

Have you tried removing the SDL_DestroyTexture function from your load image function?

No I haven’t tried them, I wanted to learn myself so I wouldn’t mindlessly copy the code that’s being written by them. Also I wrote it myself I know I’m bad at coding.

I realized I made a typo and I didn’t notice it now it works

1 Like

To be blunt: It is a bit obvious that you’re starting from inside somewhat of a vacuum (I really thought the code was AI generated because of that).
It’s true that some people go at a tutorial with the intent to do a smash and grab. But the author’s hope is that you are going to instead engage with their content and try to understand the reasoning of the methods shown, then make your own project with a bit more knowledge under your belt.

You are not bad at coding; I see the devotion to the project and you have a lot of potential.
… But your approach is going to run you into a hard wall. Could you imagine trying to implement that loadImage function with even just a dozen different animated characters and scenes?

The lazyfoo tutorials are meant to put you on a good track, they are the SDL C++ game-dev equivalent of “Hello World” to many of us here, and the techniques that they will show to you will save you months or even years of frustration.
So please read through as much of the tutorial series as you can. (It is best to try to read them in order but after you get a sense for the framework, it becomes natural to skip around to the topics that interest you.) It might take a few days or it might take a month, but it will be worth all the time that you put into it.

When you need a break from SDL but still want to learn more about game design, there’s a pretty awesome book for generic game design here → Game Programming Patterns.

2 Likes