Windows UMDF CComPtr IWDFMemory 未被释放

Windows UMDF CComPtr IWDFMemory does not get freed

在我的 UMDF 驱动程序中,我有一个 IWDFMemory 包装在 CComPtr

CComPtr<IWDFMemory> memory;

CComPtr 的文档说,如果 CComPtr 对象超出范围,它会自动释放。这意味着此代码不应造成任何内存泄漏:

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.
}

此外,如果我手动将 NULL 分配给 memory 或在范围结束前调用 memory.Release(),则内存不会被释放。我想知道这里发生了什么?

根据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.

因为你确实传递了NULL,所以在释放CComPtr<IWDFDriver>对象之前,内存不会被释放。