Click here to Skip to main content
15,890,506 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.7K   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

 
GeneralEnabling memory leak detection in VC++ Pin
elizas26-Feb-10 2:34
elizas26-Feb-10 2:34 
QuestionHow to port wince in smdk2410 board. Pin
amiya das30-Mar-08 21:41
amiya das30-Mar-08 21:41 
Questionwhat about new byte[x] and delete [] Pin
bsyossi14-Jan-07 23:29
bsyossi14-Jan-07 23:29 
GeneralC rountines Pin
markpotts16-Nov-06 0:15
markpotts16-Nov-06 0:15 
GeneralRe: C rountines Pin
Cookie FJ15-Feb-08 18:06
Cookie FJ15-Feb-08 18:06 
QuestionWhat if CMemoryDump Pin
Sreekanth Muralidharan28-Nov-05 19:18
Sreekanth Muralidharan28-Nov-05 19:18 
GeneralSolution for possible bug with delete Pin
Tommyknocker19806-Oct-05 22:48
Tommyknocker19806-Oct-05 22:48 
GeneralRe: Solution for possible bug with delete Pin
Sreekanth Muralidharan30-Nov-05 16:59
Sreekanth Muralidharan30-Nov-05 16:59 
GeneralRe: Solution for possible bug with delete Pin
lenux27-Feb-08 21:00
lenux27-Feb-08 21:00 
Generalactivation key Pin
chhavir27-Jul-05 22:17
chhavir27-Jul-05 22:17 
General&quot;new&quot; error Pin
kurtak8-Mar-05 23:22
kurtak8-Mar-05 23:22 
GeneralRe: &amp;quot;new&amp;quot; error Pin
Sreekanth Muralidharan8-Dec-05 21:59
Sreekanth Muralidharan8-Dec-05 21:59 
GeneralReports wrong filename (and line number) Pin
polgie27-May-04 22:58
polgie27-May-04 22:58 
GeneralRe: Reports wrong filename (and line number) Pin
Sreekanth Muralidharan1-Dec-05 16:39
Sreekanth Muralidharan1-Dec-05 16:39 
GeneralNow its ready for WCE MFC. Pin
Piotr Boguslawski24-Nov-03 1:56
Piotr Boguslawski24-Nov-03 1:56 
GeneralRe: Now its ready for WCE MFC. Pin
jasonpkuphy16-May-05 23:15
jasonpkuphy16-May-05 23:15 
GeneralRe: Now its ready for WCE MFC. Pin
Tommyknocker19806-Oct-05 22:52
Tommyknocker19806-Oct-05 22:52 
JokeRe: Now its ready for WCE MFC. Pin
poney22-Mar-06 23:00
poney22-Mar-06 23:00 
GeneralRe: Now its ready for WCE MFC. Pin
bsyossi14-Jan-07 22:31
bsyossi14-Jan-07 22:31 
GeneralIn Mfc ,it report inexistent memory leak Pin
jasonpkuphy25-Aug-03 17:22
jasonpkuphy25-Aug-03 17:22 
GeneralRe: In Mfc ,it report inexistent memory leak Pin
jasonpkuphy25-Aug-03 17:45
jasonpkuphy25-Aug-03 17:45 
Questionhow to use Pin
sillychai24-Dec-02 23:30
sillychai24-Dec-02 23:30 
Generalwriting a task manager in c++ Pin
Phienix20-Oct-02 11:22
sussPhienix20-Oct-02 11:22 
GeneralRe: writing a task manager in c++ Pin
Sreekanth Muralidharan8-Dec-05 17:11
Sreekanth Muralidharan8-Dec-05 17:11 
Hi
Try calling EnumProcesses() to enumerate the running processes. EnumProcesses() will get you an array of process ids. If you need the file name of the file that has spawned each process, try using GetModuleFileNameEx(). Before calling this make sure you enumerate all the modules associated with the process by calling EnumerateProcessModules().
That complete's half of your work.
After you get the handles to all the processes, from the user interface you have designed take the user input(user clicked on xxx process). Try getting the handle to that process. Manage the process from there.
If you want to terminate the process user selected, use TerminateProcess() function.

The above process WILL NOT work for smart devices.
If your project is for smart devices, use the following technique:
// SmartPhoneTaskManager.cpp : Defines the entry point for the application.<br />
//<br />
<br />
#include "stdafx.h"<br />
#include "SmartPhoneTaskManager.h"<br />
#include "Tlhelp32.h"<br />
#include <windows.h><br />
#include <commctrl.h><br />
<br />
#define MAX_LOADSTRING 100<br />
<br />
// Global Variables:<br />
HINSTANCE			g_hInst;			// current instance<br />
HWND				g_hWndMenuBar;		// menu bar handle<br />
<br />
// Forward declarations of functions included in this code module:<br />
ATOM			MyRegisterClass(HINSTANCE, LPTSTR);<br />
BOOL			InitInstance(HINSTANCE, int);<br />
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);<br />
<br />
BOOL CALLBACK TaskManagerDlgProc(const HWND hTaskManDlg, const UINT uIMessage, <br />
								 const WPARAM wParam, LPARAM lParam);<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance,<br />
                   HINSTANCE hPrevInstance,<br />
                   LPTSTR    lpCmdLine,<br />
                   int       nCmdShow)<br />
{<br />
	MSG msg;<br />
<br />
	// Perform application initialization:<br />
	if (!InitInstance(hInstance, nCmdShow)) <br />
	{<br />
		return FALSE;<br />
	}<br />
<br />
<br />
	// Main message loop:<br />
	while (GetMessage(&msg, NULL, 0, 0)) <br />
	{<br />
		{<br />
			TranslateMessage(&msg);<br />
			DispatchMessage(&msg);<br />
		}<br />
	}<br />
<br />
	return (int) msg.wParam;<br />
}<br />
<br />
//<br />
//  FUNCTION: MyRegisterClass()<br />
//<br />
//  PURPOSE: Registers the window class.<br />
//<br />
//  COMMENTS:<br />
//<br />
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)<br />
{<br />
	WNDCLASS wc;<br />
<br />
	wc.style         = CS_HREDRAW | CS_VREDRAW;<br />
	wc.lpfnWndProc   = WndProc;<br />
	wc.cbClsExtra    = 0;<br />
	wc.cbWndExtra    = 0;<br />
	wc.hInstance     = hInstance;<br />
	wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMARTPHONETASKMANAGER));<br />
	wc.hCursor       = 0;<br />
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
	wc.lpszMenuName  = 0;<br />
	wc.lpszClassName = szWindowClass;<br />
<br />
	return RegisterClass(&wc);<br />
}<br />
<br />
//<br />
//   FUNCTION: InitInstance(HINSTANCE, int)<br />
//<br />
//   PURPOSE: Saves instance handle and creates main window<br />
//<br />
//   COMMENTS:<br />
//<br />
//        In this function, we save the instance handle in a global variable and<br />
//        create and display the main program window.<br />
//<br />
HWND hTaskManagerDlg;<br />
HWND hListWnd;<br />
DWORD WINAPI ProcessViewThread(PVOID lParam);<br />
<br />
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)<br />
{<br />
    HWND hWnd;<br />
    TCHAR szTitle[MAX_LOADSTRING];		// title bar text<br />
    TCHAR szWindowClass[MAX_LOADSTRING];	// main window class name<br />
<br />
    g_hInst = hInstance; // Store instance handle in our global variable<br />
<br />
<br />
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); <br />
    LoadString(hInstance, IDC_SMARTPHONETASKMANAGER, szWindowClass, MAX_LOADSTRING);<br />
<br />
    //If it is already running, then focus on the window, and exit<br />
    hWnd = FindWindow(szWindowClass, szTitle);	<br />
    if (hWnd) <br />
    {<br />
        // set focus to foremost child window<br />
        // The "| 0x00000001" is used to bring any owned windows to the foreground and<br />
        // activate them.<br />
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));<br />
        return 0;<br />
    } <br />
<br />
    if (!MyRegisterClass(hInstance, szWindowClass))<br />
    {<br />
    	return FALSE;<br />
    }<br />
<br />
    hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,<br />
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);<br />
<br />
    if (!hWnd)<br />
    {<br />
        return FALSE;<br />
    }<br />
<br />
    ShowWindow(hWnd, nCmdShow);<br />
    UpdateWindow(hWnd);<br />
	<br />
    hTaskManagerDlg = 	CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG_TASKMAN),/*hWnd*/NULL,(DLGPROC)TaskManagerDlgProc);<br />
	if(!hTaskManagerDlg)<br />
	{<br />
		MessageBox(NULL,TEXT("Could not start taskman"),TEXT("Taskman Failed"),MB_OK|MB_ICONERROR);<br />
		return FALSE;<br />
<br />
	}<br />
<br />
	ShowWindow(hTaskManagerDlg,SW_SHOWMAXIMIZED);<br />
	UpdateWindow(hTaskManagerDlg);<br />
	hListWnd = ::GetDlgItem(hTaskManagerDlg,IDC_LIST_ENUM_TASKS);<br />
	::SetFocus(hListWnd);<br />
	DWORD threadID;<br />
	CreateThread(NULL,0,&ProcessViewThread,0,0,&threadID);<br />
<br />
	return TRUE;<br />
}<br />
<br />
//<br />
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)<br />
//<br />
//  PURPOSE:  Processes messages for the main window.<br />
//<br />
//  WM_COMMAND	- process the application menu<br />
//  WM_PAINT	- Paint the main window<br />
//  WM_DESTROY	- post a quit message and return<br />
//<br />
//<br />
<br />
<br />
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
    int wmId, wmEvent;<br />
    PAINTSTRUCT ps;<br />
    HDC hdc;<br />
	<br />
	<br />
    switch (message) <br />
    {<br />
        case WM_COMMAND:<br />
            wmId    = LOWORD(wParam); <br />
            wmEvent = HIWORD(wParam); <br />
            // Parse the menu selections:<br />
            switch (wmId)<br />
            {<br />
                case IDM_OK:<br />
                   // DestroyWindow(hWnd);<br />
					break;<br />
				case IDM_CANCEL:<br />
					DestroyWindow(hWnd);<br />
                default:<br />
                    return DefWindowProc(hWnd, message, wParam, lParam);<br />
					<br />
            }<br />
            break;<br />
        case WM_CREATE:<br />
            SHMENUBARINFO mbi;<br />
<br />
            memset(&mbi, 0, sizeof(SHMENUBARINFO));<br />
            mbi.cbSize     = sizeof(SHMENUBARINFO);<br />
            mbi.hwndParent = hWnd;<br />
            mbi.nToolBarId = IDR_MENU;<br />
            mbi.hInstRes   = g_hInst;<br />
<br />
            if (!SHCreateMenuBar(&mbi)) <br />
            {<br />
                g_hWndMenuBar = NULL;<br />
            }<br />
            else<br />
            {<br />
                g_hWndMenuBar = mbi.hwndMB;<br />
            }<br />
<br />
		     break;<br />
        case WM_PAINT:<br />
            hdc = BeginPaint(hWnd, &ps);<br />
			// TODO: Add any drawing code here...<br />
            <br />
            EndPaint(hWnd, &ps);<br />
            break;<br />
        case WM_DESTROY:<br />
            CommandBar_Destroy(g_hWndMenuBar);<br />
            PostQuitMessage(0);<br />
            break;<br />
<br />
<br />
        default:<br />
            return DefWindowProc(hWnd, message, wParam, lParam);<br />
    }<br />
    return 0;<br />
}<br />
<br />
<br />
<br />
BOOL CALLBACK TaskManagerDlgProc(const HWND hTaskManDlg, const UINT uIMessage, <br />
								 const WPARAM wParam, LPARAM lParam)<br />
{<br />
<br />
	HWND statusHandle =  GetDlgItem(hTaskManagerDlg,IDC_STATIC_STATUS);<br />
<br />
	BOOL hIsHandled = TRUE;<br />
	switch(uIMessage)<br />
	{<br />
	case WM_COMMAND:<br />
		{ <br />
			// Task manager UI event notifications here ...<br />
			switch(LOWORD(wParam))<br />
			{<br />
			case IDCANCEL:<br />
				hIsHandled = TRUE;<br />
				DestroyWindow(hTaskManagerDlg);<br />
				PostQuitMessage(0);<br />
				break;<br />
			case IDC_BUTTON_STOPPROCESS:<br />
				// Get the handle of the process user selected.<br />
				// End that process here...<br />
				<br />
				hIsHandled = TRUE;<br />
			default:<br />
				hIsHandled = FALSE;<br />
			}<br />
		}<br />
	case WM_PAINT:<br />
		SetWindowText(statusHandle,TEXT("Running..."));<br />
		break;<br />
<br />
	}<br />
<br />
	return hIsHandled;<br />
<br />
}<br />
<br />
<br />
/**<br />
	Return type: DWORD<br />
	Type: WINAPI<br />
	Function Name: ProcessViewThread<br />
	Arguments: PVOID lParam<br />
	Description: Thread for enumerating all the processes running in the system.<br />
	Dependencies: Tlhelp32.lib library<br />
*/<br />
DWORD WINAPI ProcessViewThread(PVOID lParam)<br />
{<br />
	<br />
	<br />
<br />
	static int index = 0;<br />
	HANDLE snapHand;<br />
	PROCESSENTRY32 procEntry;<br />
	procEntry.dwSize = sizeof(PROCESSENTRY32);<br />
	snapHand = CreateToolhelp32Snapshot(TH32CS_SNAPALL|TH32CS_SNAPPROCESS,0);<br />
	if(INVALID_HANDLE_VALUE == snapHand)<br />
		return -1;<br />
	if(!Process32First(snapHand,&procEntry))<br />
	{<br />
		if(GetLastError() ==  ERROR_NO_MORE_FILES)<br />
		{<br />
			SendMessage(hListWnd,LB_ADDSTRING,0,LPARAM(TEXT("EndOfListing")));<br />
		}<br />
		return -1;<br />
	}<br />
	procEntry.dwSize = sizeof(procEntry);<br />
	do<br />
	{<br />
		SendMessage(hListWnd,LB_ADDSTRING,index,LPARAM(procEntry.szExeFile));<br />
		SendMessage(hListWnd,LB_SETCURSEL,index,0);<br />
		index++;<br />
	}while(Process32Next(snapHand,&procEntry));<br />
<br />
	HWND stopHandle = GetDlgItem(hTaskManagerDlg,IDC_BUTTON_STOPPROCESS);<br />
	::SetFocus(stopHandle);<br />
<br />
	// SendMessage(hListWnd,LB_ADDSTRING,++index,LPARAM(TEXT("<Completed>")));<br />
<br />
	CloseToolhelp32Snapshot(snapHand);<br />
	<br />
	<br />
<br />
	return 0;<br />
<br />
<br />
}<br />

GeneralError during debug Pin
Anonymous11-Oct-02 0:20
Anonymous11-Oct-02 0:20 

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.