Click here to Skip to main content
Click here to Skip to main content

Memory leak detection for WinCE

By , 12 Jun 2001
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralEnabling memory leak detection in VC++groupelizas26 Feb '10 - 2:34 
QuestionHow to port wince in smdk2410 board.memberamiya das30 Mar '08 - 21:41 
Hi,
 
i am new to wince. I followed the steps from the following docs but i did not succeed.
 
http://linuxtogo.org/~koen/letux/bootloader/bsp/DOC/DotNET420Guide.html
http://linuxtogo.org/~koen/letux/doc/NET410Guide.htm
http://arm.co.kr/programs/read.php?board=qna&uid=409&cp=3&field=&keyWord=&PHPSESSID=c20fb1f1438ba974d7c2beca9a4c89c2
http://www.embedinfo.com/english/download/winces3ceb2410.pdf
 
if any one knows how to port wince to smdk2410 board please help me.
 
amiya kumar das

Questionwhat about new byte[x] and delete []memberbsyossi14 Jan '07 - 23:29 
GeneralC rountinesmembermarkpotts16 Nov '06 - 0:15 
GeneralRe: C rountinesmemberCookie FJ15 Feb '08 - 18:06 
QuestionWhat if CMemoryDumpmemberSreekanth Muralidharan28 Nov '05 - 19:18 
GeneralSolution for possible bug with deletememberTommyknocker19806 Oct '05 - 22:48 
GeneralRe: Solution for possible bug with deletememberSreekanth Muralidharan30 Nov '05 - 16:59 
GeneralRe: Solution for possible bug with deletememberlenux27 Feb '08 - 21:00 
Generalactivation keymemberchhavir27 Jul '05 - 22:17 
General&quot;new&quot; errormemberkurtak8 Mar '05 - 23:22 
GeneralRe: &amp;quot;new&amp;quot; errormemberSreekanth Muralidharan8 Dec '05 - 21:59 
GeneralReports wrong filename (and line number)susspolgie27 May '04 - 22:58 
GeneralRe: Reports wrong filename (and line number)memberSreekanth Muralidharan1 Dec '05 - 16:39 
GeneralNow its ready for WCE MFC.memberPiotr Boguslawski24 Nov '03 - 1:56 
GeneralRe: Now its ready for WCE MFC.memberjasonpkuphy16 May '05 - 23:15 
GeneralRe: Now its ready for WCE MFC.memberTommyknocker19806 Oct '05 - 22:52 
JokeRe: Now its ready for WCE MFC.memberponey22 Mar '06 - 23:00 
GeneralRe: Now its ready for WCE MFC.memberbsyossi14 Jan '07 - 22:31 
GeneralIn Mfc ,it report inexistent memory leakmemberjasonpkuphy25 Aug '03 - 17:22 
GeneralRe: In Mfc ,it report inexistent memory leakmemberjasonpkuphy25 Aug '03 - 17:45 
Questionhow to usemembersillychai24 Dec '02 - 23:30 
Generalwriting a task manager in c++sussPhienix20 Oct '02 - 11:22 
GeneralRe: writing a task manager in c++memberSreekanth Muralidharan8 Dec '05 - 17:11 
GeneralError during debugsussAnonymous11 Oct '02 - 0:20 
GeneralRe: Error during debugsussAnonymous11 Oct '02 - 2:44 
GeneralRe: Error during debugsussAnonymous21 May '05 - 11:29 
GeneralRe: Error during debugsussAnonymous23 Oct '05 - 20:07 
QuestionHow to use this application??sussmaniyar11 Jul '02 - 20:34 
GeneralIt works even in MFC....memberRoberto Campos Aliaga16 Apr '02 - 0:58 
GeneralRe: It works even in MFC....memberslomoman15 Oct '03 - 7:10 
GeneralRe: It works even in MFC....sussRoberto Campos Aliaga16 Oct '03 - 21:55 
GeneralRe: It works even in MFC....memberslomoman16 Oct '03 - 23:07 
QuestionAny problem with NKDbgPrintffW ...?memberRoberto Campos Aliaga16 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 exceptionmemberRoberto Campos Aliaga16 Apr '02 - 0:39 
GeneralSolution to this warningmemberRoberto Campos Aliaga17 Apr '02 - 22:01 
Generalerror C2660: 'new' : function does not take 3 parametersmemberGeorgia Albuquerque20 Mar '02 - 1:27 
GeneralRe: error C2660: 'new' : function does not take 3 parametersmemberAnonymous27 Mar '02 - 0:46 
GeneralRe: error C2660: 'new' : function does not take 3 parametersmemberlokmcf9 Jan '03 - 20:52 
GeneralRe: error C2660: 'new' : function does not take 3 parameterssussAnonymous17 Oct '05 - 1:10 
GeneralLIB File for crtdbg.hmemberThomas S.1 Feb '02 - 3:18 
GeneralIt work fine whit MFCmemberYossi12 Jan '02 - 10:46 
GeneralRe: It work fine whit MFCmemberAnonymous6 Mar '02 - 14:44 
GeneralLink errormemberFlorian RIVAL16 Nov '01 - 0:35 
GeneralRe: Link errormemberCiprian Miclaus16 Nov '01 - 1:01 
GeneralRe: Link errorsussAnonymous16 Sep '04 - 5:07 
GeneralPlease do not forget about realloc also.memberVctOs3 Nov '01 - 20:57 
QuestionDoes this work with MFC?memberMel28 Oct '01 - 1:47 
AnswerRe: Does this work with MFC?memberCiprian Miclaus29 Oct '01 - 0:42 
GeneralRe: Does this work with MFC?memberslomoman15 Oct '03 - 7:07 

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

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