Memory leak detection for WinCE

Environment: winCE

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. In addition it displays the ammount of free store memory that your program uses.

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 malloc

if (retPtr == (void*)0x76DA0)
   dumb instruction; <- place a breakpoint on 
                        this one, or just use DebugMsg

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__)

How it works

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. Remember this when you have static variables; you must give a global scope to the instance of garbageCollector. You can do that by placing the macro at the global scope. However, the C++ standard does not provide an order of initialization for static variables, which means you cannot assure garbageCollector will be initialized first. Therefore in case of static or global variables, it will report memory leaks for variables initialized after garbageCollector has been initialized.

Comments

Please report any bugs or feedback to Ciprian_Miclaus@yahoo.com.

Downloads

Download demo project - 11 Kb
Download source - 2 Kb


Comments

  • interested in payday loans uk lenders only

    Posted by Nuptsmeashhem on 05/02/2013 08:50am

    The short term loans are along with higher mortgage rates and they are given away for a highest period of Calendar month payday loans. Nonetheless, there is amazing assuring a safe gain of payday loans You can use this mortgage loan to make the mind up any of your financial issues such as paying out medical bills, college fees, groceries, car maintenance, house modification and many other problems After all, while it may offer a short-term solution, it is just a long-term commitment, and if you drop your home for doing it, there's no benifit of a mortgage-refinancing personal loan at all

    Reply
  • Memory leaks detection tool

    Posted by Petruha on 11/25/2009 11:12am

    The easiest way is to use some memory leaks detection tool, like Deleaker ( http://deleaker.com/ )

    Reply
  • Detect meory leak in evc++ due to malooc,calloc and free

    Posted by Legacy on 04/14/2003 12:00am

    Originally posted by: Amoli

    Can someone tell me where I can find the some code similar to this to detect meory leak in evc++ due to malloc,calloc and free as well ?

    Reply
  • Any Development Tools to Recommend for programming in WinCE??

    Posted by Legacy on 09/19/2002 12:00am

    Originally posted by: bloon

    Anyone here knows any Development Tools to Recommend for programming in WinCE?? thanx...

    Reply
  • My PDA's CPU is ARM,the demo project not work.why?

    Posted by Legacy on 08/07/2002 12:00am

    Originally posted by: ciml

    My PDA's CPU is ARM,the demo project not work.why?

    Reply
  • It is very nice!, Except for MFC classes.

    Posted by Legacy on 08/14/2001 12:00am

    Originally posted by: sung-joo byun


    Nice! It works well.
    But, It doesn't work for MFC classes.

    Because CObject class already defined 'operator new',
    So, the other 'operator new' member is hidden.

    I solve this problem follow..

    Intesad of
    #define new new(_T(__FILE__), __LINE__)
    ,

    I added scope oprator :: for specifing global scope.
    #define new ::new(_T(__FILE__), __LINE__)

    It works well...


    p.s :

    have a nice day....

    Reply
  • Only to HPC2000 and POCKET PC

    Posted by Legacy on 08/02/2001 12:00am

    Originally posted by: Dario Alves Junior

    How to do this component work with HPCPRO 2.11??

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds