Click here to Skip to main content
Licence CPOL
First Posted 12 Jun 2001
Views 193,582
Downloads 291
Bookmarked 59 times

Memory leak detection for WinCE

By | 12 Jun 2001 | Article
This code detects memory leaks in embedded VC++ almost the same way crtdbg does in VC++.

Introduction

This code detects memory leaks in embedded VC++ almost the same way crtdbg does in VC++. At the end of program execution it will display in the debug window if there were any memory leaks and how the memory looks so you can identify where your memory leak occurred. It will display in the debug window a message saying no memory leaks detected if there are no memory leaks. Similar to what crtdbg.h does in VC++. The code detects memory leaks generated with calls to new and delete operators in C++. The code doesn't detect memory leaks generated with C functions: malloc, calloc, free, but that can be done in the future. Let me know and I will program it.

There are 3 simple steps in order to enable memory leak detection:

  1. Define _DEBUG
    #define _DEBUG
  2. Include "crtdbg.h"
    #include "crtdbg.h"
  3. Let your first line in the code be:
    _CrtSetDbgFlag (ON);

Tips on debugging:

Tip 1:

Although it doesn't display the line where the memory leak occurred (read Tip 2), the utility display the address in hex, and you can add a small code to the operator new function, just after the first malloc:

	if (retPtr == (void*)0x76DA0)
		dumb instruction; <- place a breakpoint on this one

so you can detect easily which line of your code called the operator new to allocate memory at the specified address and wasn't freed.

Tip 2:

Here's a trick that allow you to get the correct line and filename where the memory leak occurred. Define the following line in every file, or define it in a header file and include it in every file where you want accurate line and filename:

#define new new(_T(__FILE__), __LINE__)

What the code actually does is override the global operator new that besides allocating memory, it retains the pointer to the allocated memory in a list. The operator delete simply releases memory and deletes the reference to that memory from the list. In the end of the program execution, all the pointers still in the list simply mean memory leaks, and they are displayed in the debug window.

The macro _CrtSetDbgFlag (ON); simply declares an instance of garbageCollector. It has to be the first line of your code, to insure it is the last variable in your program to be destroy, as in its destructor it performs checking of the pointer list and it displays memory leaks. So its destructor must be the last destructor called.

License

Public domain

Comments

Please report any bugs to Ciprian_Miclaus@yahoo.com. You can use and distribute this code freely, but please keep these few lines. If you make any improvements, or have any ideas about how this code could be improved or just you feel you need to comment this code in any way, please send your comments, idea, improvements to me to my email above.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ciprian Miclaus



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralEnabling memory leak detection in VC++ Pingroupelizas2:34 26 Feb '10  
QuestionHow to port wince in smdk2410 board. Pinmemberamiya das21:41 30 Mar '08  
Questionwhat about new byte[x] and delete [] Pinmemberbsyossi23:29 14 Jan '07  
GeneralC rountines Pinmembermarkpotts0:15 16 Nov '06  
GeneralRe: C rountines PinmemberCookie FJ18:06 15 Feb '08  
QuestionWhat if CMemoryDump PinmemberSreekanth Muralidharan19:18 28 Nov '05  
GeneralSolution for possible bug with delete PinmemberTommyknocker198022:48 6 Oct '05  
GeneralRe: Solution for possible bug with delete PinmemberSreekanth Muralidharan16:59 30 Nov '05  
GeneralRe: Solution for possible bug with delete Pinmemberlenux21:00 27 Feb '08  
Generalactivation key Pinmemberchhavir22:17 27 Jul '05  
General"new" error Pinmemberkurtak23:22 8 Mar '05  
GeneralRe: &quot;new&quot; error PinmemberSreekanth Muralidharan21:59 8 Dec '05  
GeneralReports wrong filename (and line number) Pinsusspolgie22:58 27 May '04  
GeneralRe: Reports wrong filename (and line number) PinmemberSreekanth Muralidharan16:39 1 Dec '05  
GeneralNow its ready for WCE MFC. PinmemberPiotr Boguslawski1:56 24 Nov '03  
GeneralRe: Now its ready for WCE MFC. Pinmemberjasonpkuphy23:15 16 May '05  
GeneralRe: Now its ready for WCE MFC. PinmemberTommyknocker198022:52 6 Oct '05  
JokeRe: Now its ready for WCE MFC. Pinmemberponey23:00 22 Mar '06  
GeneralRe: Now its ready for WCE MFC. Pinmemberbsyossi22:31 14 Jan '07  
GeneralIn Mfc ,it report inexistent memory leak Pinmemberjasonpkuphy17:22 25 Aug '03  
GeneralRe: In Mfc ,it report inexistent memory leak Pinmemberjasonpkuphy17:45 25 Aug '03  
Questionhow to use Pinmembersillychai23:30 24 Dec '02  
Generalwriting a task manager in c++ PinsussPhienix11:22 20 Oct '02  
GeneralRe: writing a task manager in c++ PinmemberSreekanth Muralidharan17:11 8 Dec '05  
GeneralError during debug PinsussAnonymous0:20 11 Oct '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 13 Jun 2001
Article Copyright 2001 by Ciprian Miclaus
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid