Click here to Skip to main content
12,634,030 members (24,472 online)
Click here to Skip to main content
Add your own
alternative version

Stats

223.6K views
696 downloads
63 bookmarked
Posted

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralEnabling memory leak detection in VC++ Pin
elizas26-Feb-10 3:34
groupelizas26-Feb-10 3:34 
Its a good practise if we don't depend on IDE/third-party tools/software to detect memory leaks in our code. Its better if we can predict before hand the consequences of writing each line of code.

But there may be situations where we need to work on projects written by someone else. In such cases these third-party tools may help us in finding a QUICK solution.

The visual studio debugger along with C run-time (CRT) libraries provides us means for detecting memory leaks.But its capabilities are minimal.

Enabling memory leak detection in visual studio:
1. If our program can exit from multiple locations, instead of putting a call to _CrtDumpMemoryLeaks at each possible exit,include the following call at the beginning of program:
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

This statement automatically calls _CrtDumpMemoryLeaks when program exits.

2 . The macro _CRTDBG_MAP_ALLOC applies to c library functions malloc, realloc.With a little bit of extra code, we can get it to work with new/delete:

#define _CRTDBG_MAP_ALLOC

#include <iostream>
#include <crtdbg.h>

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

These macros should be added after including all other header files.

3. By default, _CrtDumpMemoryLeaks dumps memory leak information to the Output window,we can reset this to a file using _CrtSetReportMode. For doing this add the following lines of code at the starting of your program.

HANDLE hLogFile;
hLogFile = CreateFileA("c:\\memoryLeaksDump.rtf", GENERIC_WRITE,FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);

_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, hLogFile);
_CrtSetReportFile(_CRT_ERROR, hLogFile);
_CrtSetReportFile(_CRT_ASSERT, hLogFile);

_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

http://www.mindfiresolutions.com/Enabling-memory-leak-detection-in-VC-44.php[^]
Cheers,
Eliza

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 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.

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