Tracking down memory leaks

Started by
4 comments, last by Simplicity 18 years, 6 months ago
I've run into a bit of a snag with my game. Somewhere, resources aren't being freed when I close the game and after running my program many times, I find that the free memory my computer has is gone, and I get a strange delayed input effect when I run my game, where pressing a button doesn't get an effect until about half a second later. Knowing that the memory leaks are there is one thing, but tracking them down is troublesome. Are there any programs or features of MSVC++ that I can use to make sure that I free memory that I allocate?
Advertisement
Programs: BoundsChecker is the main one, but it's a commercial package so you'll have to purchase it (unless they still offer a time limited trial version).

Programming: Run your application in a debugger and use a memory manager which will help you detect and trace memory leaks. As luck would have it, the MSVC C runtime library has built in support for this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/_core_Solving_Buffer_Overwrites_and_Memory_Leaks.asp?frame=true

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Creating a memory manager for your source base is a good idea, here is a tutorial on the steps needed to create one. There are also several libs floating around the internet that are usually very easy to plug into most programs.

http://www.flipcode.com/articles/article_memoryleaks.shtml
Quote:Original post by starstriker1
I've run into a bit of a snag with my game. Somewhere, resources aren't being freed when I close the game and after running my program many times, I find that the free memory my computer has is gone, and I get a strange delayed input effect when I run my game, where pressing a button doesn't get an effect until about half a second later.

Knowing that the memory leaks are there is one thing, but tracking them down is troublesome. Are there any programs or features of MSVC++ that I can use to make sure that I free memory that I allocate?
Memory allocated within a process's heap is automatically freed (under Windows) when the process exits.

Are you running on Windows?
If you are, and you are closing your program each time, then you must be using up some kind of system resource, and new/delete trackers such as Paul Nettle's memory manager may not be able to help you much.

btw it might also pay to check the task manager and verify that the previous instances of the program have all quit when you think they have.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by pants
Creating a memory manager for your source base is a good idea, here is a tutorial on the steps needed to create one. There are also several libs floating around the internet that are usually very easy to plug into most programs.

http://www.flipcode.com/articles/article_memoryleaks.shtml


I -very- highly recommend this artical. I've used it in one incarnation or another for many projects and found it the only overloading new/delete to work consistantly. A word of warning, it _will_ slow down your app quite a bit if you have a lot of dynamic allocation, so having a way to easily remove it is handy, and I definately wouldn't leave it in on a release build.

Creating a memory manager for your source base is a good idea, here is a tutorial on the steps needed to create one. There are also several libs floating around the internet that are usually very easy to plug into most programs.

http://www.flipcode.com/articles/article_memoryleaks.shtml


The funny thing from the article is that the override new calls AddTrack which in turns calls new again. Seems like there will be recursion problem. I suggest replacing new with malloc.

[Edited by - Simplicity on October 23, 2005 9:59:19 AM]

This topic is closed to new replies.

Advertisement