6

I have an extreme problem.

I have been working on a game for about two years(20000+ lines of code), and Lately I have been noticing a ton of memory leaks. The problem is that I cannot track every single one of them since my game is way too big...

I have searched around and noticed that CppCheck would be useful in my situation, but the problem is that since I am using windows, i cannot use CppCheck(which is for linux only).

I am wondering if maybe there is a library or plugin that is CppCheck's equivalent for windows, or maybe a way to use CppCheck on windows instead.

All of the possibilities that I have come up with, along with solutions to other's problems(such as using smart pointers for std::deque and such) imply that my program is small or the more fitting: rewrite my entire program, something that I -really- do not want to do...

IDE: Code Blocks 10.05

Compiler: MinGW 3.81 GCC 4.4.1

versat
  • 137
  • 2
  • 14
Molma
  • 171
  • 9
  • As other information: I have skills that I use that are the main bottlenecks for the memory leaks(1 hour of extreme combat and the game goes from 100mb to 1.5gb). Other situations are moving from map to map(despawns other NPCs/deletes from vector) – Molma Jan 13 '13 at 23:02
  • I used CppCheck. On Windows. I found it to be an open source project in great need of programmers that need to it to be better. Given the alternatives, I didn't see much point in helping out. – Hans Passant Jan 13 '13 at 23:03
  • 8
    This is why you start with smart pointers, not start with raw pointers and find you need smart pointers after your program is 20000 lines of code. – Seth Carnegie Jan 13 '13 at 23:06
  • Would you happen to know any other programs to find memory leaks? If not then maybe even a library or method to store copies of NPCs for said map(deleting them afterwards) – Molma Jan 13 '13 at 23:06
  • 1
    @Molma: The trouble with any tool like this is that it's not very precise -- it might be able to catch *some* memory leaks, but it's very unlikely for it to be able to catch *all* of them. What I would suggest is another approach: start commenting which pointers in your code *own* memory, and which ones merely *reference* it. Take special note of circular references if there are any. Then, one by one, start using `shared_ptr`, and converting to (owning) pointers using `release` in order to keep the program compilable. Keep repeating until you get rid of leaks or have no more raw pointers. – user541686 Jan 13 '13 at 23:08
  • 3
    @SethCarnegie When I first started working on this project, I had no ideas of smart pointers or memory leaks. It was only recently that I have noticed the massive amounts of leaks that my program has(lately I have been bent on increasing speed). I blame this crazy mistake on myself since I am self-taught... – Molma Jan 13 '13 at 23:10
  • 1
    Also see http://vld.codeplex.com/ – user541686 Jan 13 '13 at 23:11
  • @Molma: It's all a learning experience. :) Just curious, do you use `malloc`/`free` in your code a lot, or `new` and `delete`? – user541686 Jan 13 '13 at 23:12
  • @Mehrdad If it were that simple then I would have started doing that already. My problem against that is the fact that most of my code(centered around the player) would not agree to `shared_ptr`. The way I have my `deque`s coded are where the player is also added to the list of spawns, this way the skills and other objects can react to it just like any other NPC – Molma Jan 13 '13 at 23:14
  • @Molma: That's why I didn't tell you to use `shared_ptr` everywhere; I said start using it *incrementally* (starting at wherever you create new objects points), switching to raw pointers when you need to, by releasing the `shared_ptr` and getting back a raw pointer. – user541686 Jan 13 '13 at 23:15
  • @Mehrdad `malloc/free` I did not know much about, `new/delete` is what I use for almost every object I have in my game. Im doubting that the problem is with creating objects, I would say its more toward any type of `deque` storage. – Molma Jan 13 '13 at 23:17
  • 2
    @Molma what you need to do is look for every `new` and find where and when it is `delete`d in your source. If this really is your first project, it's unreasonable not to expect to rewrite large portions as your knowledge grows. – Seth Carnegie Jan 13 '13 at 23:20
  • @Mehrdad Im pretty sure in the past I have had problems with changing slight bits of `deque` to `shared_ptr`. And for some reason, When changing anything to `shared_ptr` it said that the object's name was not defined, meanwhile it was defined on that exact spot... – Molma Jan 13 '13 at 23:20
  • 1
    @SethCarnegie: Easier said than done at this point to be fair. That's why it's important to try and avoid memory leaks in the first place. – Stuart Golodetz Jan 13 '13 at 23:21
  • @Molma: If possible would you mind showing me an example of a few lines that you changed from `deque` to `shared_ptr`, which gave an error? It shouldn't be erroring like that. – user541686 Jan 13 '13 at 23:26
  • the first error I got was in one of my header files. `std::deque ItemList` to `std::deque> ItemList`. Also I am not sure as to keep Item* when changing to `shared_ptr` – Molma Jan 13 '13 at 23:32
  • @Molma what compiler are you using? If it's moderately modern, it shouldn't be in the `tr1` namespace, just the `std` namespace. – Seth Carnegie Jan 13 '13 at 23:32
  • @SethCarnegie Im still trying to find that, I have gotten the default install with codeBlocks 10.05, but I am having trouble finding the version for MinGW – Molma Jan 13 '13 at 23:34
  • The version is 3.81 MinGW GCC 4.4.1 – Molma Jan 14 '13 at 00:06
  • There isn't going to be a magic bullet here, you already know the answer (use smart pointers) and anything less than that is going to be a band-aid solution, so I'm not sure what sort of answer you are hoping for here. Perhaps clarify your question with an example piece of code you'd like to avoid rewriting? – Sam Miller Jan 14 '13 at 18:04
  • Alright, after some research I have found some information that should make converting to `shared_ptr` a bit easier: [shared_ptr question](http://stackoverflow.com/questions/3476938/example-to-use-shared-ptr). with this I should be able to switch to `shared_ptr` quickly and without much fail. One question though: When changing to `shared_ptr` should it be in `shared_ptr` format or `shared_ptr` format? with the first I am wondering if that will still work with variables with that type of pointer(e.g. Ogre::SceneNode*), since I use ogre3d – Molma Jan 16 '13 at 03:34

1 Answers1

5

CppCheck works on Windows too (check downloads on SourceForge). CppCheck is only a static check tool (it analyzes your source code to find some potential problems). In order to find real memory leaks it may be necessary to use some debugging tool that actually runs your code (look at Google's Dr. Memory for example).

sirgeorge
  • 6,331
  • 1
  • 28
  • 33