Click here to Skip to main content
15,881,775 members
Articles / Mobile Apps

Memory leak detection for WinCE

Rate me:
Please Sign up or sign in to vote.
3.83/5 (16 votes)
12 Jun 2001CPOL2 min read 255.3K   822   64   50
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Error during debug Pin
Anonymous11-Oct-02 2:44
Anonymous11-Oct-02 2:44 
GeneralRe: Error during debug Pin
Anonymous21-May-05 11:29
Anonymous21-May-05 11:29 
GeneralRe: Error during debug Pin
Anonymous23-Oct-05 20:07
Anonymous23-Oct-05 20:07 
QuestionHow to use this application?? Pin
Zubby11-Jul-02 20:34
Zubby11-Jul-02 20:34 
GeneralIt works even in MFC.... Pin
16-Apr-02 0:58
suss16-Apr-02 0:58 
GeneralRe: It works even in MFC.... Pin
slomoman15-Oct-03 7:10
slomoman15-Oct-03 7:10 
GeneralRe: It works even in MFC.... Pin
rca_correo16-Oct-03 21:55
rca_correo16-Oct-03 21:55 
GeneralRe: It works even in MFC.... Pin
slomoman16-Oct-03 23:07
slomoman16-Oct-03 23:07 
QuestionAny problem with NKDbgPrintffW ...? Pin
16-Apr-02 0:48
suss16-Apr-02 0: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
16-Apr-02 0:39
suss16-Apr-02 0:39 
GeneralSolution to this warning Pin
17-Apr-02 22:01
suss17-Apr-02 22:01 
Generalerror C2660: 'new' : function does not take 3 parameters Pin
20-Mar-02 1:27
suss20-Mar-02 1:27 
GeneralRe: error C2660: 'new' : function does not take 3 parameters Pin
27-Mar-02 0:46
suss27-Mar-02 0:46 
GeneralRe: error C2660: 'new' : function does not take 3 parameters Pin
lokmcf9-Jan-03 20:52
lokmcf9-Jan-03 20:52 
GeneralRe: error C2660: 'new' : function does not take 3 parameters Pin
Anonymous17-Oct-05 1:10
Anonymous17-Oct-05 1:10 
GeneralLIB File for crtdbg.h Pin
1-Feb-02 3:18
suss1-Feb-02 3:18 
GeneralIt work fine whit MFC Pin
12-Jan-02 10:46
suss12-Jan-02 10:46 
GeneralRe: It work fine whit MFC Pin
6-Mar-02 14:44
suss6-Mar-02 14:44 
GeneralLink error Pin
16-Nov-01 0:35
suss16-Nov-01 0:35 
GeneralRe: Link error Pin
16-Nov-01 1:01
suss16-Nov-01 1:01 
GeneralRe: Link error Pin
Anonymous16-Sep-04 5:07
Anonymous16-Sep-04 5:07 
GeneralPlease do not forget about realloc also. Pin
3-Nov-01 20:57
suss3-Nov-01 20:57 
QuestionDoes this work with MFC? Pin
Ancient Dragon28-Oct-01 1:47
Ancient Dragon28-Oct-01 1:47 
AnswerRe: Does this work with MFC? Pin
29-Oct-01 0:42
suss29-Oct-01 0:42 
GeneralRe: Does this work with MFC? Pin
slomoman15-Oct-03 7:07
slomoman15-Oct-03 7:07 

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

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