0

I've been working on and off on a game of mine for the last 2 years. I am developing it using SDL2.0 in Visual Studio. Since the first months of working on it I had realized that it had some memory leaks and I have finally decided to deal with them. I don't know why it is happening since I have checked multiple times that I am deallocating any memory that I am allocating. Either with new/delete or with SDL's functions.

The first thing I did was to check the memory profiler and I was indeed right. The next thing I did was to open the heap profiler and I can see that the allocations and deallocations of objects on the heap are all working as expected. The only thing that seems to be increasing steadily over time is the Unresolved allocations block. I am clicking on it but I can't really find any useful information about the source of these allocations. Is there a way I can trace their source?

The project is kinda big and I don't know what part of the code I should include for this. For anyone who wants to take a look at the code it is at https://github.com/PanosPetras/The_Great_War. I am suspecting that maybe some of the drawables are at fault but I don't have any evidence to support it (Probably the Button and the Label).

  • Are you asking for someone to download your project and find how it's leaking memory? If so, this is very unlikely to happen. – Sam Varshavchik Apr 07 '23 at 11:11
  • Not really, first off I want more info on what Unresolved allocations are. I couldn't find anything around the web for it. What are they caused by? Why don't they have any more info? How can I find more info on them. And the link for the project is there if you want to take a look, you don't have to download it. It's on github. – Panos Petrakopoulos Apr 07 '23 at 11:13
  • I took a peek at your project and you use `new` a lot. Try using it zero times and it'll be more likely to not leak. Also, don't initialize `int`s with `NULL`. Use `0`. `NULL` is for pointers - but don't use `NULL` for pointers. Use `nullptr`. – Ted Lyngmo Apr 07 '23 at 11:17
  • Unresolved allocations are allocated memory that the profiler could not determine where they were allocated. It's a memory leak. They're caused by memory leaks. The memory profiler is a fairly basic tool and lacks full instrumentation that other, more sophisticated tools use to track exactly where each allocated memory block was created. – Sam Varshavchik Apr 07 '23 at 11:17
  • My guess is that you could avoid the down-votes by reducing the text in the question to focus on just the basic question: "what are unresolved allocations" ? – Mike Nakis Apr 07 '23 at 11:22
  • Thank you for all the information I will try to use it to solve the problem. – Panos Petrakopoulos Apr 07 '23 at 11:29
  • 1
    `delete PauseButton, Date, SpeedBg, SpeedImg;` etc... This leaks `Date`, `SpeedBg` and `SpeedImg`. Replace all your raw owning pointers with `std::unique_ptr`s. – Ted Lyngmo Apr 07 '23 at 11:30
  • @PanosPetrakopoulos I was bored so I made [a few changes](https://github.com/PanosPetras/The_Great_War/pull/2) that fixes some of the leaks and shows how you could use `unique_ptr`. Your usage of `std::bind` wasn't correct in some places so I replaced those with lambdas. I also fixed some issues with mixing lowercase/uppercase letters when including headers. That works in Windows but not in Linux etc. So, now it compiles in Linux too. I wasn't able to run it though since I don't have the libraries needed. – Ted Lyngmo Apr 07 '23 at 13:14
  • 1
    @TedLyngmo I saw your commit and I just want to say thank you for your help. I am grateful. I will try to improve it based on your recommendations. – Panos Petrakopoulos Apr 08 '23 at 18:07
  • @PanosPetrakopoulos You're welcome. When I ran it with the AddressSanitizer it reported problems so I may provide fixes for those too when I get som time. – Ted Lyngmo Apr 08 '23 at 18:16

1 Answers1

0

The way to solve issues of this kind is to introduce your own memory allocator (overload new) and have it store extra information in each memory block, for example the __FILE__ and __LINE__ of the code that invoked new.

I would guess that there must be some ready-made library out there that will do that for you.

Keep in mind, however, that in the gaming industry people generally try to avoid using new.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Thank you for the information! I will try to find an appropriate library to help me locate it. I will also try to reduce my use of 'new'. Since you mentioned it, why do the gaming industry people try to avoid 'new'? – Panos Petrakopoulos Apr 07 '23 at 11:28
  • Because using `new` tends to be slower than not using `new`. – Mike Nakis Apr 07 '23 at 11:30
  • Note that this does not mean "never use `new`"; it means "try to avoid `new` unless necessary". – Mike Nakis Apr 07 '23 at 11:31