How to detect memory leaks in UWP C++

Started by
7 comments, last by DividedByZero 7 years, 2 months ago

Hi guys,

I have purposely introduced a leak in to my UWP C++ (VS.net 2015) application with the intent of figuring out how to detect leaks on exit.

So far, I can't figure out how to detect leaks on exit. The internet seems pretty sparse on that front.

Any advice would be greatly appreciated :)

Advertisement

I use something like described in this blogpost as same as unit tests and profiling to detect memory leaks

http://bitsquid.blogspot.de/2010/09/custom-memory-allocation-in-c.html

I use visual leak detector, works easy and tells you more about the leaks in the debugger output (if there are any)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

One trick I find really useful is to add the following at the start of your main function:


#ifdef _DEBUG
	// The following code is used for detecting memory leaks. If a memory leak is detected when the application exits,
	// the information will be printed out to the debug output window. Change the -1 in _CrtSetBreakAlloc( -1 ) to the
	// allocation number and run the code again... easy to tell if bits and pieces aren't being cleaned up
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );

	_CrtSetBreakAlloc( -1 );
#endif

You'll get a list of allocations that haven't been freed up when your program exits, then you just replace the -1 with the allocation number in _CrtSetBreakAlloc() so you can potentially see what was being allocated.

This certainly isn't foolproof, but it may help you out.

Thanks for the help, guys! Some things to check out :)


One trick I find really useful is to add the following at the start of your main function:

#ifdef _DEBUG
	// The following code is used for detecting memory leaks. If a memory leak is detected when the application exits,
	// the information will be printed out to the debug output window. Change the -1 in _CrtSetBreakAlloc( -1 ) to the
	// allocation number and run the code again... easy to tell if bits and pieces aren't being cleaned up
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );

	_CrtSetBreakAlloc( -1 );
#endif
You'll get a list of allocations that haven't been freed up when your program exits, then you just replace the -1 with the allocation number in _CrtSetBreakAlloc() so you can potentially see what was being allocated.

This certainly isn't foolproof, but it may help you out.


Wow! I didn't know this would still work under UWP. I use this method a lot in my normal C++ projects.

Thanks again everyone! :)

Wow! I didn't know this would still work under UWP. I use this method a lot in my normal C++ projects.

Hah hah, I can't guarantee that it will - worth a shot though :)

Wow! I didn't know this would still work under UWP. I use this method a lot in my normal C++ projects.

Hah hah, I can't guarantee that it will - worth a shot though :)

Just gave it a go. Unfortunately it didn't detect any leaks for anything that was created with 'new' but not deleted :(

Maybe UWP does some sort of forcible cleanup after an app has ended? I don't really know - LOL.

I assume you've tried the basic results from a google search?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I assume you've tried the basic results from a google search?


You assume correctly. ;)

From the link, it appears that it will catch memory leaks during program execution, which is pretty basic to chase at the best of times. But, I am not seeing how to catch leaks caused by not deleting new'ed classes etc.

This topic is closed to new replies.

Advertisement