locked
Finding memory leaks in Windows Store App

    General discussion

  • Hello,

    My Windows Store App written in C++ is leaking memory. I wonder if there is a tool that would tell me the stack traces of memory leaks so that I may release them to prevent leakage.

    Below posts do not provide an answer.

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/4b0f6c3d-de7b-414f-9043-9a81a6abcb98

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/cfc397f5-7ac4-4ac8-949b-e96ea2168541

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/843485ef-3e54-448a-a019-ed9bf06fb57f

    Thanks.

    Friday, March 01, 2013 10:35 AM

All replies

  • Those blog links are all useful, but have you tried the more basic stuff:

    - Enabling the CRT DEBUG heap

    - Running Application Verifier's Heaps test?

    Friday, March 01, 2013 7:28 PM
  • Thanks for the answer. I already tried "CRT DEBUG heap" with below code before leaking part starts:

        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

        _CrtSetDbgFlag(tmpFlag | _CRTDBG_LEAK_CHECK_DF);

    and below after leaking part ends.

    _CrtDumpMemoryLeaks().

    I only get entries like below which does not include stack trace or similar regarding the source of the leak.

    {139} normal block at 0x00A17DE0, 96 bytes long.
     Data: < L `         %  > B8 4C FC 60 CD CD CD CD 02 00 00 00 00 25 A1 00

    Also my leak is around mega bytes according to the "Task Manager" but dumped entries are summing up only a few kilo bytes.

    ---

    I could not find how Application Verifier could be used to find memory leak. Also it looks it is for ".exe" files. How is that used for Windows Store Apps?

    Friday, March 01, 2013 9:43 PM
  • Not so recently I also had a leak, and I couldn't find with CRT debug... The problem was - incorrect usage of WinRT type, where ref counts for WinRT types weren't decreasing though the WinRT object should have died, the problem is WinRT types are being allocated on global heap.

    In any case - here's what I did, and this helped me in some way:

            PROCESS_MEMORY_COUNTERS_EX e;
    	assert(sizeof(e) == sizeof(c));
    	BOOL r = GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&e, sizeof(e));

    GetProcessMemoryInfo will return the same data, you see in the Task Manager, allowing you to see the memory changes at runtime.

    Now, you'll probably say - GetProcessMemoryInfo isn't available for Windows Store Apps, and you're absolutely right ! So here's what you can do:

    * Create a simple Win32 static library, create a function where you're using GetProcessMemoryInfo to extract all the needed data.

    * Link the static library to your Windows Store Apps, use you're newly created function.

    * And voila, you can now track how global heap changes.

    Sure, you won't be able to pass WACK, but for development purposes, I think it's totally fine to use hack like this.

    See also - functions like - GetProcessHeaps, GetProcessHeap, HeapWalk.

    Hope that helps

    Update:

    Oh yeah, regarding my leak, I did something like this:

    {
      Platform::String^ myArray[10]; <-- Wrong, should have used Platform::Array
    
      memset(myArray, 0, sizeof(myArray)); <-- This is also not good, because I am ignoring AddRef/Release mechanism
    
      myArray[0] = ref new Platform::String("Blabla"); <-- implictly calls AddRef behind the scenes
    
      // Release is never called for my newly created string, thus producing leak, unless I explictly do this myArray[0] =nullptr
    }


    Sunday, March 03, 2013 9:32 PM
  • Hi Tomas,

    Thanks for the useful info. My app mainly uses DirectX and I am guessing the main leaky part is there. It is a huge stack and I do not think I can find leaks by getting memory usage snapshots in different points. I need a tool to tell me the allocated but not released memory.

    Monday, March 04, 2013 2:14 PM
  • For debugging leaking Direct3D resources, use the Debug Layer.
    Monday, March 04, 2013 8:07 PM
  • Hi Chuck,

    I think my problem could be more general one. I am not releasing any DirectX resources. Do you have any example code in which the DirectX resources (device, context, etc) are released in correct order?

    Tuesday, March 05, 2013 7:53 AM
  • It appears that MSFT has a tool to diagnose memory leaks and more, "NF .NET profiler".

    http://www.microsoft.com/en-us/download/details.aspx?id=35370

    http://blogs.msdn.com/b/webapps/archive/2012/11/23/troubleshooting-memory-leaks-in-windows-store-apps.aspx

    Unfortunately the tool does not work for me. It stuck in “Waiting for profiler to load …”. Anyone know what is wrong?

    Thanks.

    Tuesday, March 05, 2013 3:32 PM
  • "NF .NET profiler" works OK for C# Windows Store Apps but appears to be NOT supporting C++/CX Windows Store Apps. So there is still no tool to find memory leaks for Windows Store Apps written in C++/CX.
    Tuesday, March 05, 2013 7:26 PM
  • Generally if you are using WRL::ComPtr for the DirectX objects, it should just all work out. The only tricks here are:

    (A) You'll want to call ClearState() on your context before cleanup begins

    (B) You want to make sure that the D3D11Device itself is released last. Typically you do this by having the WRL::ComPtr declared first in the class list of members.

    Wednesday, March 06, 2013 8:55 PM
  • (B) Yes, we are using WRL::ComPtr for the DirectX objects (actually I am using StarterKit as a base http://code.msdn.microsoft.com/windowsapps/Visual-Studio-3D-Starter-455a15f1).

    (A) Calling ClearState before cleanup decreased the leak dramatically. It was around 16MBs before and now it is 2-3MBs. Thanks!!

    BTW: Debug Layers is activated but I am not seeing any message on the immediate window regarding a memory leak or so.

    Thursday, March 07, 2013 9:10 AM