在我的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(),则内存不会被释放。我想知道这里发生了什么事?
发布于 2016-09-23 18:15:39
根据MSDN
如果在pParentObject参数中指定NULL,则驱动程序对象将成为新创建的内存对象的默认父对象。如果UMDF驱动程序创建了驱动程序与特定设备对象、请求对象或其他框架对象一起使用的内存对象,则驱动程序应该适当地设置内存对象的父对象。当父对象被删除时,内存对象及其缓冲区被删除。
由于确实传递NULL,所以在释放CComPtr<IWDFDriver>对象之前不会释放内存。
https://stackoverflow.com/questions/39661026
复制相似问题