Re: _CrtSetDbg to look for memory management errors

  • From: "Will Pearson" <will@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 30 Oct 2007 23:24:02 -0000

Hi Chris,

As it sounds as though you are getting some heap corruption then page heap memory may also be a useful technique for you to investigate. Page heap memory places either some filler bytes or an inaccessible page after each memory allocation and will throw an exception when things attempt to write to these or they otherwise get messed up. Page heap memory comes in two flavours: standard and full. Standard is the flavour that uses the filler bytes and full is the flavour that uses inaccessible pages. My experience has been that the full flavour is better.

Page heap memory is a feature of Windows, and so you don't need to modify your code to use it. To turn it on you need to set one of the image execution options that are found under a registry key. The easiest way to set these options is to use the Global Flags tool that comes as part of Microsoft's Debugging Tools For Windows package. To turn on full page heap for an application you can use the following command line:
gflags /p /enable my application.exe /full
and to disable page heap you can use:
gflags /p /disable myapplication.exe
where myapplication.exe is the name of the application that you want to enable page heap for. At any time you can check what applications have page heap enabled using the following command line:
gflags /p

This technique is pretty useful. I was using it myself earlier on today to track down a bug related to freeing heap allocations in a module other than the one that allocated the memory.

----- Original Message ----- From: "Delaunay Christophe" <christophe.delaunay@xxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Tuesday, October 30, 2007 2:33 PM
Subject: _CrtSetDbg to look for memory management errors

Hi list,

I'm developping in C++ with Visual Studio 2003 and am currently
experiencing a critical memory management problem. I've written a really
annoying bug somewhere in my code which cause some of my buffers to be

I'd like to do the following in order to track my bug(s):

(1) Call _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF) at the beginning of my
main module in order to thoroughly check the heap of my app during its

(2) Redirect the so-called "debug heap manager" messages to some file
other than the Visual Studio Output Window.

This is this second operation I don't know how to perform. Please could
someone explain me which calls I should insert into my code to perform
this debug heap manager output and error redirection?

Many thanks in advance. Have a nice day. ChD
View the list's information and change your settings at
View the list's information and change your settings at //

Other related posts: