Click here to Skip to main content
11,430,402 members (76,048 online)
Click here to Skip to main content

Memory leak detection for WinCE

, 12 Jun 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Ciprian Miclaus

United States United States
No Biography provided

Comments and Discussions

 
GeneralEnabling memory leak detection in VC++ Pin
elizas26-Feb-10 3:34
groupelizas26-Feb-10 3:34 
QuestionHow to port wince in smdk2410 board. Pin
amiya das30-Mar-08 22:41
memberamiya das30-Mar-08 22:41 
Questionwhat about new byte[x] and delete [] Pin
bsyossi15-Jan-07 0:29
memberbsyossi15-Jan-07 0:29 
GeneralC rountines Pin
markpotts16-Nov-06 1:15
membermarkpotts16-Nov-06 1:15 
GeneralRe: C rountines Pin
Cookie FJ15-Feb-08 19:06
memberCookie FJ15-Feb-08 19:06 
QuestionWhat if CMemoryDump Pin
Sreekanth Muralidharan28-Nov-05 20:18
memberSreekanth Muralidharan28-Nov-05 20:18 
GeneralSolution for possible bug with delete Pin
Tommyknocker19806-Oct-05 23:48
memberTommyknocker19806-Oct-05 23:48 
GeneralRe: Solution for possible bug with delete Pin
Sreekanth Muralidharan30-Nov-05 17:59
memberSreekanth Muralidharan30-Nov-05 17:59 
GeneralRe: Solution for possible bug with delete Pin
lenux27-Feb-08 22:00
memberlenux27-Feb-08 22:00 
Generalactivation key Pin
chhavir27-Jul-05 23:17
memberchhavir27-Jul-05 23:17 
General&quot;new&quot; error Pin
kurtak9-Mar-05 0:22
memberkurtak9-Mar-05 0:22 
GeneralRe: &amp;quot;new&amp;quot; error Pin
Sreekanth Muralidharan8-Dec-05 22:59
memberSreekanth Muralidharan8-Dec-05 22:59 
GeneralReports wrong filename (and line number) Pin
polgie27-May-04 23:58
susspolgie27-May-04 23:58 
GeneralRe: Reports wrong filename (and line number) Pin
Sreekanth Muralidharan1-Dec-05 17:39
memberSreekanth Muralidharan1-Dec-05 17:39 
GeneralNow its ready for WCE MFC. Pin
Piotr Boguslawski24-Nov-03 2:56
memberPiotr Boguslawski24-Nov-03 2:56 
GeneralRe: Now its ready for WCE MFC. Pin
jasonpkuphy17-May-05 0:15
memberjasonpkuphy17-May-05 0:15 
GeneralRe: Now its ready for WCE MFC. Pin
Tommyknocker19806-Oct-05 23:52
memberTommyknocker19806-Oct-05 23:52 
JokeRe: Now its ready for WCE MFC. Pin
poney23-Mar-06 0:00
memberponey23-Mar-06 0:00 
GeneralRe: Now its ready for WCE MFC. Pin
bsyossi14-Jan-07 23:31
memberbsyossi14-Jan-07 23:31 
GeneralIn Mfc ,it report inexistent memory leak Pin
jasonpkuphy25-Aug-03 18:22
memberjasonpkuphy25-Aug-03 18:22 
GeneralRe: In Mfc ,it report inexistent memory leak Pin
jasonpkuphy25-Aug-03 18:45
memberjasonpkuphy25-Aug-03 18:45 
Questionhow to use Pin
sillychai25-Dec-02 0:30
membersillychai25-Dec-02 0:30 
Generalwriting a task manager in c++ Pin
Phienix20-Oct-02 12:22
sussPhienix20-Oct-02 12:22 
GeneralRe: writing a task manager in c++ Pin
Sreekanth Muralidharan8-Dec-05 18:11
memberSreekanth Muralidharan8-Dec-05 18:11 
GeneralError during debug Pin
Anonymous11-Oct-02 1:20
sussAnonymous11-Oct-02 1:20 
GeneralRe: Error during debug Pin
Anonymous11-Oct-02 3:44
sussAnonymous11-Oct-02 3:44 
GeneralRe: Error during debug Pin
Anonymous21-May-05 12:29
sussAnonymous21-May-05 12:29 
GeneralRe: Error during debug Pin
Anonymous23-Oct-05 21:07
sussAnonymous23-Oct-05 21:07 
QuestionHow to use this application?? Pin
maniyar11-Jul-02 21:34
sussmaniyar11-Jul-02 21:34 
GeneralIt works even in MFC.... Pin
Roberto Campos Aliaga16-Apr-02 1:58
memberRoberto Campos Aliaga16-Apr-02 1:58 
GeneralRe: It works even in MFC.... Pin
slomoman15-Oct-03 8:10
memberslomoman15-Oct-03 8:10 
GeneralRe: It works even in MFC.... Pin
Roberto Campos Aliaga16-Oct-03 22:55
sussRoberto Campos Aliaga16-Oct-03 22:55 
GeneralRe: It works even in MFC.... Pin
slomoman17-Oct-03 0:07
memberslomoman17-Oct-03 0:07 
QuestionAny problem with NKDbgPrintffW ...? Pin
Roberto Campos Aliaga16-Apr-02 1:48
memberRoberto Campos Aliaga16-Apr-02 1:48 
Generalarning C4291: 'void *__cdecl operator new(unsigned int,unsigned short *,int)' : no matching operator delete found; memory will not be freed if initialization throws an exception Pin
Roberto Campos Aliaga16-Apr-02 1:39
memberRoberto Campos Aliaga16-Apr-02 1:39 
GeneralSolution to this warning Pin
Roberto Campos Aliaga17-Apr-02 23:01
memberRoberto Campos Aliaga17-Apr-02 23:01 
Generalerror C2660: 'new' : function does not take 3 parameters Pin
Georgia Albuquerque20-Mar-02 2:27
memberGeorgia Albuquerque20-Mar-02 2:27 
GeneralRe: error C2660: 'new' : function does not take 3 parameters Pin
Anonymous27-Mar-02 1:46
memberAnonymous27-Mar-02 1:46 
GeneralRe: error C2660: 'new' : function does not take 3 parameters Pin
lokmcf9-Jan-03 21:52
memberlokmcf9-Jan-03 21:52 
GeneralRe: error C2660: 'new' : function does not take 3 parameters Pin
Anonymous17-Oct-05 2:10
sussAnonymous17-Oct-05 2:10 
GeneralLIB File for crtdbg.h Pin
Thomas S.1-Feb-02 4:18
memberThomas S.1-Feb-02 4:18 
GeneralIt work fine whit MFC Pin
Yossi12-Jan-02 11:46
memberYossi12-Jan-02 11:46 
GeneralRe: It work fine whit MFC Pin
Anonymous6-Mar-02 15:44
memberAnonymous6-Mar-02 15:44 
GeneralLink error Pin
Florian RIVAL16-Nov-01 1:35
memberFlorian RIVAL16-Nov-01 1:35 
GeneralRe: Link error Pin
Ciprian Miclaus16-Nov-01 2:01
memberCiprian Miclaus16-Nov-01 2:01 
GeneralRe: Link error Pin
Anonymous16-Sep-04 6:07
sussAnonymous16-Sep-04 6:07 
GeneralPlease do not forget about realloc also. Pin
VctOs3-Nov-01 21:57
memberVctOs3-Nov-01 21:57 
QuestionDoes this work with MFC? Pin
Mel28-Oct-01 2:47
memberMel28-Oct-01 2:47 
AnswerRe: Does this work with MFC? Pin
Ciprian Miclaus29-Oct-01 1:42
memberCiprian Miclaus29-Oct-01 1:42 
GeneralRe: Does this work with MFC? Pin
slomoman15-Oct-03 8:07
memberslomoman15-Oct-03 8:07 

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.

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