My game crashes

Hi, folks! :victory_hand:t3: My game crashes when I’m trying to launch this. And I already know where’s the problem.

Here’s the source code of my project: https://gitlab.com/xolatgames/birdy-wants-crisps

Here’s the main menu script in this project: https://gitlab.com/xolatgames/birdy-wants-crisps/-/blob/main/Birdy-wants-crisps/src/main_menu/main_menu.cpp?ref_type=heads

The main menu script has creating the game title script: Birdy-wants-crisps/src/ui/game_title.cpp · main · xolatgames / Birdy wants crisps · GitLab

But when it try to create a label for the game title: https://gitlab.com/xolatgames/birdy-wants-crisps/-/blob/main/Birdy-wants-crisps/src/ui/label.cpp?ref_type=heads

The game crashes.

Also the game has the separate script for loading some stuffs such as images, fonts, and also it creates some bodies for objects: Birdy-wants-crisps/src/common/utils.cpp · main · xolatgames / Birdy wants crisps · GitLab

The script for creating labels uses it.

I know where’s the problem, and when the game crashes, but I don’t know how to solve this.

How can I solve this, folks? :worried:

P.S: Sorry about I changed some links to simple text. Newcommers can’t share more then two links at the same time :sweat_smile:

Which exact line does it crash? (a debugger such as gdb will be able to tell you)

The problem is in your LoadImage function in resources.hpp/.cpp.

The LoadImage function takes in a pointer to an Image.
When you’re passing in an image to set the loaded image data to, the function won’t actually store the loaded data to the image being passed in.

This means that every time the LoadImage function is called, the loaded data is not stored properly to the Image being passed in.

So your BG and GameIcon (among other data) isn’t properly set.

I have two suggestions on how you can fix this.

Suggestion 1:

Have the LoadImage function take in a double pointer, so a pointer to a pointer to an image to set the loaded image data to.
The image that you then pass in will have to be passed in as a reference.

Example code:

void Resources::LoadImage(Image** to_var, SDL_Renderer* rend, const char* filename)
{
	Image* img = Birdy::LoadImage(rend, filename);
	*to_var = img;
	allImgs.push_back(img);
}

The function would then be used like this:
LoadImage(&GameIcon, rend, "../icon.svg");

Suggestion 2:

Have the LoadImage function return the image data instead of setting the image data to a passed in image.

Example code:

Image* Resources::LoadImage(SDL_Renderer* rend, const char* filename)
{
	Image* img = Birdy::LoadImage(rend, filename);

	allImgs.push_back(img);

	return img;
}

The function would then be used like this:
BG = LoadImage(rend, std::string(IMAGES_FOLDER + "main_menu/background.svg").c_str());
GameIcon = LoadImage(rend, "../icon.svg");

I would personally use the second suggestion.

You have the same issue with most images that’s being loaded in the constructor of Resources. Most of them are nullptr upon loading, except the images that resides inside the multiple std::vector’s that you’re using in your code.

You’ll notice later during development that your LoadSound function is having the same issue as your LoadImage function. The LoadSound function takes in a pointer to a Mix_Chunk and the function won’t store the loaded data to the Mix_Chunk being passed in.
So you’ll need to fix that function too, for the footStepSnds->grass, for example, to be set properly.

Another thing that I want to mention.
You have a lot of memory leaks in your code.
When I shutdown the game, I get a report of around 49.000 memory leaks. You have a lot of new allocations in your code, which is not being cleaned up upon game shutdown.
Remember that for every new in your code, there should be a pairing delete for the data being allocated using new to be cleaned up properly.

Also don’t forget to call SDL_FreeSurface for every created SDL_Surface, SDL_DestroyTexture for every created SDL_Texture and the same for other SDL-specific data types, like fonts, sounds etc.

2 Likes

The fourteenth in

https://gitlab.com/xolatgames/birdy-wants-crisps/-/blob/main/Birdy-wants-crisps/src/ui/game_title.cpp

Thanks a lot! :grinning_face_with_smiling_eyes: I also want to use second suggestion.

But how did you see a number of memory leaks? :thinking:

In addition to this post:

https://cboard.cprogramming.com/cplusplus-programming/182326-my-game-crashes.html

:thinking:

I use Visual Studio as IDE and it has memory-leak-detection functionality built-in.
I don’t know if it exists in other IDE’s.

So I’ll try to think of something then. Thanks a lot again :handshake:t3:

my experience on Windows, sanitizers available for clang on MSYS2 are not reliable to catch memory leaks. surprisingly, using Emscripten to make a Web build with sanitizers will catch every memory leak.

Nowadays I use an external library to catch memory leaks and it is accurate enough for me.

but what library? something custom? during my first semester, our teacher showed a simple way to catch leaks with some malloc/calloc/free replacement functions. however, I think his technique breaks in a multi-threaded scenario, while sanitizers not