2016-09-23 21 views
0

在我UMDF司機,我有包裝內CComPtr的Windows UMDF但是CComPtr IWDFMemory沒有得到釋放

CComPtr<IWDFMemory> memory; 

CComPtr文檔說一個IWDFMemory,如果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. 
} 

另外,如果我手動分配NULLmemory或範圍之前調用memory.Release()結束內存不會被釋放。我想知道這裏發生了什麼?

回答

1

根據MSDN

如果pParentObject參數中指定NULL,則驅動程序對象 變爲用於新創建的存儲器對象的默認父對象。 如果UMDF驅動程序創建驅動程序與特定設備對象,請求對象或其他框架對象一起使用的內存對象,則 驅動程序應該適當地設置內存對象的父對象。 當父對象被刪除時,內存對象及其緩衝區 被刪除。

既然你確實傳遞NULL,內存不會被釋放,直到CComPtr<IWDFDriver>對象被釋放。

+0

那麼這個例子的好習慣是什麼?我希望內存在範圍的最後被釋放。如果不是駕駛員本身,我應該選擇什麼作爲父對象? – arminb

+0

我對UMDF並不熟悉,但我研究瞭如何使用IWDFDriver :: CreateWDFMemory,並且在指定非空所有者的地方,典型的所有者是IWDFIoRequest對象。 –