IntroductionNot 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.
|