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

Generator Code 39 in C# 17: The Debug C Run-Time Library and Memory Management

17: The Debug C Run-Time Library and Memory Management
Printing Code-39 In Visual C#
Using Barcode creation for .NET Control to generate, create Code 39 image in .NET framework applications.
www.OnBarcode.com
Code 3 Of 9 Scanner In C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
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 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. Features of the Debug C Run-Time Library The main claim to fame for the DCRT library is its wonderful heap memory tracking support. All the memory allocated through standard C/C++ memory allocation functions, such as new, malloc, and calloc, can be tracked in debug builds. The tracking checks for memory underwrites, in which your program writes past the beginning of a block of memory, and memory overwrites, in which your program writes past the end of a block of memory. All those errors are reported through an assertion from the DCRT itself. The tracking also monitors and reports memory leaks in your application when the application ends because of calls to OutputDebugString, whose output appears in the Output window of the debugger. If you've been writing Microsoft Foundation Class (MFC) library programs, you've probably noticed the memory leak reports when your application ends; those leak reports come from the DCRT library. MFC partially turns on the DCRT library for you automatically. 616
Bar Code Creator In Visual C#
Using Barcode encoder for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Scan Barcode In Visual C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Another neat feature of the DCRT library is the reporting subsystem, which you and I would call trace, through its _RPTn and RPTFn macros and assertion support. I introduced the DCRT library assertion support in 3 and explained some of the issues associated with using it. As I mentioned, the DCRT library assertions are good, but they destroy the last error value and therefore could cause debug and release builds to behave differently. For your assertions, I strongly recommend that you use the SUPERASSERT assertion code that's part of BUGSLAYERUTIL.DLL. Another nice feature of the DCRT library is that its source code is included with the compiler. Table 17-1 lists all the files that make up the DCRT library. If you elected to install the CRT library source code when you installed Microsoft Visual Studio .NET, which I highly recommend doing, you can find all the CRT and DCRT library source code in the <Visual Studio .NET Installation Directory>\VC7\CRT\SRC directory.
Drawing Code 39 Full ASCII In .NET
Using Barcode maker for ASP.NET Control to generate, create Code 3/9 image in ASP.NET applications.
www.OnBarcode.com
Code 3 Of 9 Drawer In .NET
Using Barcode drawer for .NET Control to generate, create Code 3 of 9 image in VS .NET applications.
www.OnBarcode.com
Table 17-1: Debug C Run.Time Library Source Files Source File DBGDEL.CPP DBGHEAP.C DBGHOOK.C DBGINT.H DBGNEW.CPP DBGRPT.C CRTDBG.H Description The debug global delete operator. All the debug heap-handling functions. The stub memory allocation hook function. The internal debug headers and functions. The debug global new operator. The debug reporting functions. The header file you include. This file is in the standard include directory.
Drawing Code-39 In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Code 39 Full ASCII image in .NET framework applications.
www.OnBarcode.com
Painting Bar Code In Visual C#.NET
Using Barcode creator for .NET Control to generate, create barcode image in .NET framework applications.
www.OnBarcode.com
Common Debugging Question: Why do I need the debug C run-time library if I'm using an error detection tool like BoundsChecker Error detection tools such as Compuware's BoundsChecker and Rational Software's Purify automatically handle memory underwrites, memory overwrites, and memory leaks. If you use one of these tools, you might think that using the DCRT library isn't worth your time and effort. Technically, that's true, but to ensure that you find all your memory problems, you need to run your application under your error detection tool every time anyone on the team runs a debug build of the application. "Anyone" includes you, your fellow developers, and if you followed my advice from 2, even your quality engineers. I just don't see everyone being that vigilant. Using the DCRT library is like having good fire or theft insurance. You hope you'll never need to use the insurance, but if you do, it can be a lifesaver. Any time you can validate data in your application, you should. The DCRT library doesn't impose a drastic performance hit, and it can unearth some outstanding bugs. You should always use it even if you have all the tools in the world. Using the Debug C Run-Time Library The first step in using the .DCRT library is to include it and turn it on so that you can start benefiting from the memory tracking as early in your project as possible. In your main precompiled header file (or whatever header file all the source files in your project will 617
Code-128 Generation In C#.NET
Using Barcode drawer for .NET framework Control to generate, create Code 128B image in Visual Studio .NET applications.
www.OnBarcode.com
Print PDF417 In C#
Using Barcode creator for VS .NET Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
include), add the following line before any #include directive in your precompiled header file: #define _CRTDBG_MAP_ALLOC After the rest of your header files, include CRTDBG.H. Defining _CRTDBG_MAP_ALLOC will redirect normal calls to memory allocation and deallocation functions to special versions that record the source file and line number for each allocation or deallocation. The second step you need to take is to turn on the DCRT library heap code. As I mentioned at the beginning of this chapter, by default, most of the features of the DCRT library are turned off. The documentation states that most of the features are turned off to keep the code small and to increase execution speed. Although size and speed are important for a release build, the whole point of a debug build is to find bugs! The increased size and reduced speed of debug builds is inconsequential. So don't hesitate to turn on all the features you think will help you. The _CrtSetDbgFlag function takes a set of flags, shown in Table 17-2, that can be OR'd together to turn on various options in the DCRT library.
Create Bar Code In Visual C#.NET
Using Barcode generator for Visual Studio .NET Control to generate, create barcode image in .NET framework applications.
www.OnBarcode.com
Draw Standard 2 Of 5 In Visual C#
Using Barcode creator for .NET framework Control to generate, create Code 2/5 image in .NET framework applications.
www.OnBarcode.com
Table 17-2: Debug C Run.Time Library Flags Flag _CRTDBG_ALLOC_MEM_DF Description Turn on the debug heap allocations, and use the memory block identifiers. This is the only flag that's on by default. Check and validate all memory on each allocation and deallocation request. Turning on this flag catches any underwrites and overwrites as close to when they happen as possible. Include _CRT_BLOCK memory allocations in all leak detection and state differences. In general, unless you're having a problem with the CRT library functions, you shouldn't turn on this flag. If you do, you'll get CRT library memory allocation reports. Because the CRT library must keep some memory allocated until the true end of the program, which is after the leaked memory reporting, you'll see a large number of false positive leak reports on that memory. Instead of truly freeing memory, keep the block allocated and in the internal heap list. The blocks are filled with the value 0xDD, so you know the memory is freed when you're looking at it in the debugger. By not freeing the memory, this flag allows you to test your program under memory stress conditions. Additionally, the DCRT library will check that you don't access the deallocated block again by ensuring that all values in the block remain 0xDD. You should always turn on this flag, but keep in mind that your program's memory 618
Encode European Article Number 13 In Objective-C
Using Barcode maker for iPhone Control to generate, create EAN13 image in iPhone applications.
www.OnBarcode.com
Drawing PDF417 In None
Using Barcode generation for Software Control to generate, create PDF-417 2d barcode image in Software applications.
www.OnBarcode.com
Reading Bar Code In Visual C#
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Creating UPC A In Java
Using Barcode maker for BIRT reports Control to generate, create UCC - 12 image in Eclipse BIRT applications.
www.OnBarcode.com
Recognizing Denso QR Bar Code In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Drawing Code 128 Code Set A In Objective-C
Using Barcode drawer for iPad Control to generate, create Code 128 Code Set B image in iPad applications.
www.OnBarcode.com
Barcode Reader In Visual Studio .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
GTIN - 128 Encoder In Java
Using Barcode drawer for BIRT Control to generate, create UCC - 12 image in BIRT applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.