0

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?

4

1 回答 1

1

根据MSDN

如果在 pParentObject 参数中指定了 NULL,则驱动程序对象将成为新创建的内存对象的默认父对象。如果 UMDF 驱动程序创建驱动程序与特定设备对象、请求对象或其他框架对象一起使用的内存对象,则驱动程序应适当地设置内存对象的父对象。当父对象被删除时,内存对象及其缓冲区也被删除。

由于您确实传递了 NULL,因此在释放对象之前不会释放内存CComPtr<IWDFDriver>

于 2016-09-23T18:15:39.260 回答