Find Memory Leaks Under WinCE

How can I track down memory leaks in my C++ application under WinCE 2013?

There are some tools in the platformbuilder (PB). I don’t know any tools that would do that without PB.

In case you don’t have PB, you can get a trial version of it from here. You may need the BSP and Workspace from our images, you can get them from the image downloads section.

There is a nice descritpion for WEC 7, which still should match for WEC 2013.

Here some direct links to some tools that are helpful:

Absolutely perfect. Thank you so much!

I would concentrate on looking at the Remote Resource Leak Detector. It allows you to take snapshots of your application and compare heap allocations.

Documentation is scant from Microsoft:

https://msdn.microsoft.com/en-us/library/gg155986.aspx

Some techniques might be applicable from here: https://msdn.microsoft.com/en-us/library/aa446911.aspx – though the Application Verifier is now the Remote Resource Leak Detector.

I’ve not tried this, but it looks interesting : http://www.codeguru.com/cpp/w-p/ce/article.php/c3513/Memory-leak-detection-for-WinCE.htm

If I think of anything else I’ll add another comment. There are a couple of minor things – platform builder has a couple of commands that allow you to dump heaps, which is also possible to do in code.

Cheers

John

Okay, I’m actually looking for leaks too. The above article from codeguru is spot on, however to find out the file and line number the macro needs to changed.

#include "stdafx.h"
#define _DEBUG
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, (wchar_t*)__FILE__, __LINE__)
#define new DEBUG_NEW
#endif
#include <crtdbg.h>
#include <string>

int wmain(int argc, wchar_t *argv[])
{
    int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);

    tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
    _CrtSetDbgFlag(tmpFlag);
	printf("Welcome to Windows Embedded Project System \n");

    void* ptr = new int[1000];

    // wait till exit or call this to check for leaks
    //_CrtDumpMemoryLeaks();
    return 0;
}

will give you this sort of output

Detected memory leaks!
Dumping objects ->
TestMemoryEmbedded.cpp(21) : {859} normal block at 0x00126350, 4000 bytes long.
 Data: <††††††††切㽡䉼䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄> 䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄䑃䌠⁄
Object dump complete.
The program '[105335494] TestMemoryEmbedded.exe' has exited with code 0 (0x0).

Notice the helpful file and line number.

Cheers

John

You can also break when the memory gets allocated.

If the block is allocated at the same point, you can use
_CrtSetBreakAlloc(n);
where n is the order that the allocation was made. In the above example, it would be
_CrtSetBreakAlloc(859);

Hi John

Thanks for the link. Tried the crtdbg.h from codeguru but I can’t compile it. Get a lot of errors (tried with MFC and non-MFC). Have you changed something? Do you use the Toradex SDK? or can you attach the crtdbg.h file?