• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    23
  • comments
    15
  • views
    29718

Memory Leak Woes

Sign in to follow this  
Followers 0
SpeedRun

1497 views

[subheading]Finding Memory Leaks[/subheading]
If you are not a big company, chances are you cannot afford the commercial programs to check for memory leaks in your program.. This tutorial will give you a free alternative which you can add to your projects.

The 1st thing that we need to do is to include . One thing to note is that crtdbg.h should be added after all the other header files

To make debugging easier, it would be nice if we could get the file and the line no which is causing the leak. Luckily, this can easily be done with some macro trickery
#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif

The _DEBUG macro is defined only for release builds so by encasing the code in #ifdef/#endif quotes, we can ensure that we dont get sub-par code with Release Builds.
_NORMAL_BLOCK is the ordinary memory allocated by our program. Alternatively, we can use _CLIENT_BLOCK which is a special type of memory block (typically used by MFC programs) for objects that require a destructor.
__FILE__ and __LINE___ are predefined ANSI C macros which return The name of the current source file and the line number the current source file respectively.
Now all of our new operator calls three parameter new calls with the __FILE__ and __LINE__ automatically inserted by the pre-compiler.

We can override the new and delete functions to add our own custom tracking methods. We just need to make sure they atleast do what the old new/delete operator functions did.

The only thing remaining is to set the debug flags to enable memory leak detection. Make sure you call this function at the start of your program
[spoiler]void CheckForMemoryLeaks()
{
#ifdef _DEBUG
// Get Current flag
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) ;

// Turn on leak-checking bit
flag |= (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF) ;

// Set flag to the new value
_CrtSetDbgFlag(flag) ;
#endif _DEBUG
[/spoiler]

The _CrtSetDbgFlag function allows the application to control how the debug heap manager tracks memory allocations by modifying the bit fields of the _crtDbgFlag flag.OR'ing the current _crtDbgFlag flag with _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF and setting it, the program automatically calls _CrtDumpMemoryLeaks when the program exits.

The _CrtDumpMemoryLeaks function determines whether a memory leak has occurred since the start of program execution. When a leak is found, the debug header information for all the objects in the heap is dumped in a user-readable form.

If you do not set _CRTDBG_LEAK_CHECK_DF, you will need to call _CrtDumpMemoryLeaks yourself.

[subheading]Additional Reading[/subheading]
You can read more about the different flags here

[subheading]Coming Up[/subheading]
In the next tutorial, we will look at creating a window

1
Sign in to follow this  
Followers 0


4 Comments


Good stuff! I'm usually good at keeping track of what has been allocated into memory, but cluster happens the larger your project gets. I find keeping a piece of paper with anything being allocated, and mark when it was deleted. My paper method isn't as easy when projects get really big. This is why I keep a lot of comments in my code for what is going on. It's the price you pay for control. :)

Looking forward to your next entry!
0

Share this comment


Link to comment
Black-Rook : Try adding the above code snippets to your project. Although, commenting code is always good (to an extent, )You might not need to use a paper to track memory. Personally, I find that if you know the offending line of code, its easy to fix the leak by freeing the memory in the appropriate place.
1

Share this comment


Link to comment
I must say I'm impressed on the usefulness of CRT Debugging. I found a lot of nice reads online about this, and I've never really known this existed until reading your entry; maybe I did but paid no attention.

I look forward to more of your posts SpeedRun!
0

Share this comment


Link to comment
Glad you found it helpful [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now