Monday, March 7, 2011

Visual C++ 2010: Detecting Memory Leaks For Global Variables

This article is a hint for those who feel desperate with finding the origin of a memory leak in their programs. I suppose you've already read the Microsoft's documentation on this topic - Finding Memory Leaks Using the CRT Library - and set up your project to enable leak detection.

The documentation states that if you did everything properly, the Output window should display all the leaked memory blocks along with source file names and line numbers. However sometimes it's not the case and there's nothing shown except a bare memory address and allocation size. And you end up staring at these numbers, puzzling over what you could do wrong with leak detection setup, placing exit()s all over the code and trying to understand logically where those damned leaks could originate from.

Actually this might be not your fault that you don't see line numbers. To be precise, it's not your fault but it's a problem of your project's design. Probably your project uses many global variables which get initialized long before source line number tracking is in effect and thus in the end debug CRT detects leaks but cannot report line numbers. Global variable usage is unavoidable in large and complex projects so we need some method to fix such leaks.

The good news is that such method exists. The bad news is that it involves much manual work. But at least it works.
  • First you'll need to make a whole program run to get the entire memory leak report in the Output window. Copy the memory leak report somewhere, e.g. to a Notepad window, as later you'll need these numbers in curly braces called memory allocation numbers.
  • Open the crt0dat.c file in Visual Studio. I assume that during the Visual C++ installation you had chosen the default folder, so that file should be located in "C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src".
  • Within the crt0dat.c file, search for the following string (no quotes): "__cdecl _initterm_e". Place a breakpoint at the first statement of the _initterm_e() function.
  • Run your program again. The execution stops at your breakpoint. Now go to the Watch window and type "_crtBreakAlloc" (no quotes) in the Name column. In the Value column most probably you'll see -1.
  • Disable the breakpoint in crt0dat.c. You won't need it to be hit again during this program run.
  • Now get back to your Notepad window and copy to the clipboard the first of the memory allocation numbers in curly braces. Go to the Watch window in Visual Studio and in the Value column replace the value shown with the value in the clipboard. Press Enter.
  • Resume the execution by hitting F5 or choosing Continue from the menu. After a while Visual Studio should display a message that reads "<YourProgram> has triggered a breakpoint" and stops at some location within the CRT debug code, most likely in the dbgheap.c file.
  • Now go to the Call Stack window and scan it from the top to the bottom until you find a function that is known to be written by you. Now you can conclude on what can be the reason of the memory leak. It might turn so that at the top or in the middle of the call stack there are gray lines containing only addresses. This means that symbol information is absent for some libraries used in your project. Hit Shift-F11 until you get rid of gray lines before the known functions. Ignore "No source code available" messages if they appear and keep hitting Shift-F11.
  • Once you finished your investigation with one of the leaks, you may continue with another without restarting the program. Just get the next memory allocation number from the previous memory leak report, paste it into the Value column for _crtBreakAlloc in the Watch window and hit F5. Investigate the cause of the leak. Repeat these steps until you examine all leaks you have. This works thanks to that memory leaks reported in the same order as the corresponding memory allocations happen.
Why do we need a breakpoint inside crt0dat.c? Because need to capture a memory allocation event before our main() function starts. Once main() is entered, all global variable initializations already happened and we've lost the chance to track allocation events either by hardcoding allocation numbers (using statements like "_crtBreakAlloc = 1234;") or by editing the _crtBreakAlloc watch value during the debug suspend mode. The _initterm_e() function just seems to be a good choice to place a breakpoint.

It is crucial to run your program every time with the same input conditions so as to memory allocation numbers stay unchanged between runs. Once you fixed a leak, you'll need to repeat the whole process from the start as allocation numbers likely have changed.

Hope this info will help fixing your very own leaks.

7 comments:

Etamar said...

Good article, helped me with my very same condition. many thanks.

Girish Mallya said...

Wow! Thank you so much...I was looking everywhere to try and get _crtBreakAlloc to work, but in vain. Your steps worked like a charm.

John Byrd said...

Well done; better than Microsoft's own documentation for dealing with memory allocated before main().

Coder said...

I use the same approach as explained by you (very nicely). Problem is that the number mentioned in curly braces is not constant with each run of my program. It keeps on changing. In this case I get false reports of places of memory leaks. Please help how to proceed in this case?

Dmitri Silaev said...

@Coder
That means your code is not deterministic. I.e. due to some external conditions, the order of execution changes for each run. Not that many chances to debug such memory leaks in this case. Either identify a specific sample of input data for which your program always runs in the same way, or rewrite your program in a [more] deterministic manner.

Master Sales said...

This is very useful article really!
Thanks for this.

And share my experience with visual studio 2015:
You can find "_initterm_e" in the line 227 of file "exe_common.inl".

roney shaheen said...

عميل كشف تسربات المياة الكريم تتسائل من اين تأتى التسربات وتظل تبحث وتتجول فى المنزل لترى من اين يأتي تسرب المياة بدون تعب
شركة كشف تسربات المياه بالمدينة المنورة
ومشقة اتصل بنا لتدلك كشف تسربات المياه بالمدينة المنورة على المكان الذى يتسرب منه الماء انت الان تتسائل انت صاحب البيت ولم تعرف من اين يتسرب الماء فكيف سنعرف نحن اقول لك عزيزى العميل ان شركة كشف تسربات تستورد احدث الاجهزة المستخدمة فى المجال والتى تدلك على مكان التسرب وليس هذا فقط كشف تسربات المياه لديها اجهزة تصليح للاضرار ايضا اسأل عن افضل شركة كشف تسربات المياه اسم له تاريخ فى مجالات النظافة
كشف تسربات المياه بالمدينة المنورة
والتسربات والتسليك وكل شيء عام الان اتصل بنا تصلك شركة كشف تسربات المياه لباب منزلك او فلتك او شقتك او قصرك.