Search code examples
memory-leakscomdrivermsdnumdf

Windows UMDF CComPtr IWDFMemory does not get freed


In my UMDF driver i have a IWDFMemory packed inside a CComPtr

CComPtr<IWDFMemory> memory;

The documentation of CComPtr says, If a CComPtr object gets out of scope, it gets automagically freed. That means this code should not create any memory leaks:

void main()
{
    CComPtr<IWDFDriver> driver = /*driver*/;
    /* 
      driver initialisation
    */

    {
        // new scope starts here

        CComPtr<IWDFMemory> memory = NULL;

        driver->CreateWdfMemory(0x1000000, NULL, NULL, &memory);
        // At this point 16MB memory have been allocated.
        // I can verify this by the task manager.

        // scope ends here
    }

    // If I understand right the memory I allocated in previous scope should already
    // be freed at this point. But in the task manager I still can see the 16 MB
    // memory used by the process.
}

Also if I manually assign NULL to memory or call memory.Release() before scope end the memory does not get freed. I am wondering what is happening here?


Solution

  • According to MSDN:

    If NULL is specified in the pParentObject parameter, the driver object becomes the default parent object for the newly created memory object. If a UMDF driver creates a memory object that the driver uses with a specific device object, request object, or other framework object, the driver should set the memory object's parent object appropriately. When the parent object is deleted, the memory object and its buffer are deleted.

    Since you do indeed pass NULL, the memory won't be released until the CComPtr<IWDFDriver> object is released.