# My game crashes

**URL:** <https://discourse.libsdl.org/t/my-game-crashes/60365>\
**Category:** Game Development\
**Created:** [July 13, 2025, 2:59pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365 "2025-07-13T14:59:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![xolatgames](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/xolatgames/32/5834_2.png) [@xolatgames](https://discourse.libsdl.org/u/xolatgames)\
**Post date:** [July 13, 2025, 2:59pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/1 "2025-07-13T14:59:11Z")

</div>

Hi, folks! ✌🏼 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](https://gitlab.com/xolatgames/birdy-wants-crisps/-/blob/main/Birdy-wants-crisps/src/ui/game_title.cpp?ref_type=heads)

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](https://gitlab.com/xolatgames/birdy-wants-crisps/-/blob/main/Birdy-wants-crisps/src/common/utils.cpp?ref_type=heads)

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? 😟

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

---

<div class="post-metadata">

**Author:** ![Peter87](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f07891/32.png) [@Peter87](https://discourse.libsdl.org/u/Peter87)\
**Post date:** [July 13, 2025, 5:53pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/2 "2025-07-13T17:53:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [July 14, 2025, 12:36am UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/3 "2025-07-14T00:36:44Z")

</div>

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![xolatgames](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/xolatgames/32/5834_2.png) [@xolatgames](https://discourse.libsdl.org/u/xolatgames)\
**Post date:** [July 14, 2025, 8:27pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/4 "2025-07-14T20:27:27Z")

</div>

The fourteenth in

[https://gitlab.com/xolatgames/birdy-wants-crisps/-/blob/main/Birdy-wants-crisps/src/ui/game\_title.cpp](https://gitlab.com/xolatgames/birdy-wants-crisps/-/blob/main/Birdy-wants-crisps/src/ui/game_title.cpp)

---

<div class="post-metadata">

**Author:** ![xolatgames](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/xolatgames/32/5834_2.png) [@xolatgames](https://discourse.libsdl.org/u/xolatgames)\
**Post date:** [July 14, 2025, 8:34pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/5 "2025-07-14T20:34:35Z")

</div>

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

But how did you see a number of memory leaks? 🤔

---

<div class="post-metadata">

**Author:** ![xolatgames](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/xolatgames/32/5834_2.png) [@xolatgames](https://discourse.libsdl.org/u/xolatgames)\
**Post date:** [July 14, 2025, 8:37pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/6 "2025-07-14T20:37:06Z")

</div>

In addition to this post:

[https://cboard.cprogramming.com/cplusplus-programming/182326-my-game-crashes.html](https://cboard.cprogramming.com/cplusplus-programming/182326-my-game-crashes.html)

🤔

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [July 14, 2025, 8:47pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/7 "2025-07-14T20:47:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xolatgames](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/xolatgames/32/5834_2.png) [@xolatgames](https://discourse.libsdl.org/u/xolatgames)\
**Post date:** [July 14, 2025, 8:51pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/8 "2025-07-14T20:51:17Z")

</div>

So I’ll try to think of something then. Thanks a lot again 🤝🏼

---

<div class="post-metadata">

**Author:** ![elefantinho](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@elefantinho](https://discourse.libsdl.org/u/elefantinho)\
**Post date:** [July 15, 2025, 12:41pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/9 "2025-07-15T12:41:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [July 15, 2025, 1:02pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/10 "2025-07-15T13:02:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elefantinho](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@elefantinho](https://discourse.libsdl.org/u/elefantinho)\
**Post date:** [July 17, 2025, 6:33pm UTC](https://discourse.libsdl.org/t/my-game-crashes/60365/11 "2025-07-17T18:33:54Z")

</div>

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
