Chapter 17: The Debug C Run-Time Library and Memory Management


Overview

Even after I wrote the first edition of this book, I got many, many questions about memory corruption and memory leaks, and I still do. If developers would just stop using memory in their programs, they wouldn't have so many problems. Right. And if we'd all stop breathing, we'd never have any trouble with our lungs. Memory is the elixir of life for your C and C++ programs, and if you want to do more about memory corruption and memory leaks than just wish they would go away, you need to be proactive about handling them. The first step you need to take is to learn how to use the Microsoft debug C run-time library.

The complexities of memory debugging are legendary and one of the prime motivators for Microsoft to develop the .NET concept. With memory taken care of for you because of the common language runtime (CLR) garbage collector, .NET eliminates probably 50 percent of the bugs you'd normally deal with in the Microsoft Win32 and Microsoft Win64 world. Of course, if raw speed is vital to your application, you'll be dealing with C++ and potential memory woes for a long time to come.

Developers working in C and C++ get to have all the fun of wrangling memory. C and C++ offer all sorts of programming freedom, but they also provide the opportunity to do much more than just shoot yourself in the foot; you can blow off your whole leg when you make even a small mistake. Fortunately, the C run-time (CRT) library developers heard our cries of anguish and, starting a million Internet years ago with Microsoft Visual C++ 4, have built in a wonderful feature: the debug CRT (DCRT) library.

Oddly enough, many C and C++ developers don't seem to realize that the DCRT library exists. The reason the library is something of a mystery is that by default much of it is turned off. Once you get the proper flags switched on, however, you'll find out what you've been missing. In this chapter, I'll start by introducing the DCRT library and build two extensions, MemDumperValidator and MemStress, that will provide you with even more functionality while you're using the library. After introducing the DCRT, I'll discuss the various issues related to memory debugging such as the operating system heaps, how to track down wild writes, and how to use these free and amazing tools from Microsoft that help you with memory issues: the PageHeap and the Application Verifier. Finally, as I promised in Chapter 2, I'll cover the amazing run-time check (/RTCx) and security (/GS) switches now offered by Visual C++ to make your memory debugging and security better than ever.




Debugging Applications for Microsoft. NET and Microsoft Windows
Debugging Applications for MicrosoftВ® .NET and Microsoft WindowsВ® (Pro-Developer)
ISBN: 0735615365
EAN: 2147483647
Year: 2003
Pages: 177
Authors: John Robbins

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net