Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
63 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Introduction
 Class Structure
 The Problem
 The Solution

 Printable version
 Discuss this article
 in the forums


Introduction

Not too long ago I was coding a game engine. Oh, it had everything; sound was automated, 3d meshes of varying formats were loaded in nicely consolidated classes, I had font classes and sprites for 2d animation, and all in all it was about an eight thousand-line project. It was fairly solid, had never crashed on me, and I thought I was ready to create a game.

Then I ran it through Microsoft's Visual C++ debugger. Darn that Microsoft. When the program shut down, a half-dozen warning messages showed up in the debug output. It was just a bunch of random hexadecimal numbers and some techno-crap jargon that compilers give you. I thought nothing of it until it started happening every time I ran the program.

It was telling me that I had memory leaks. Not too many of them, just a few. A half-dozen leaks of a few kilobytes apiece. It was nothing a modern machine couldn't handle, and my Windows 2000 machine cleaned up very nicely after my program.

Then, as I do far too often, I caught myself. There I was again, thinking I could just blow off a few warnings and random crashes. I had promised myself that this particular engine was going to be done right. Completely object-oriented and catching all possible errors. It was the first time I'd slowed myself down and forced myself to code an entire large project the right way.

So I set myself to finding those memory leaks. I scoured all my code, looked at every place I was doing a 'new' and making sure there was a corresponding 'delete.' I started using those useful SAFE_DELETE and SAFE_DELETE_ARRAY macros that are in the DirectX samples (check if the pointer is NULL, then delete it if it isn't and set it NULL). I still, once in awhile, got those memory leaks.

So I slowed down, took a good, long look at my code and my class structure, and over the course of the next few days, came up with this near-foolproof solution to my problem.



Next : Class Structure