0

For testing purposes, I put the code into a for loop which creates and releases one thousand Direct2D devices but it never free up the allocated memory:

for (UINT i = 0; i < 1000; i++) {
    hr = m_lpD2DFactory->CreateDevice(lpDxgiDevice, &m_lpD2DDevice);
    m_lpD2DDevice->Release();
}

Execution of the code above holds around 500MB of memory. Seems like releasing the device has no effect.

I appreciate any help.

Update

I have found out that if I uncomment rest of my code which leads to a pair of calls to ID2DRenderTarget::BeginDraw and ID2DRenderTarget::EndDraw using the created device, the Release method works as expected. It is still strange and could be problem if somehow my code does not lead to those calls.

hr = m_lpD2DFactory->CreateDevice(lpDxgiDevice, &m_lpD2DDevice);
...
lpRenderTarget->BeginDraw();    
lpRenderTarget->EndDraw();
...
m_lpD2DDevice->Release();
Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
  • Have you tried calling [`m_lpD2DDevice->ClearResources()`](https://msdn.microsoft.com/en-us/library/windows/desktop/hh404542.aspx) before calling `Release()`? Maybe it won't make a difference. What is the return value of `Release()`? Ideally, it should always be 0 in your example. – Remy Lebeau Dec 13 '17 at 20:34
  • @Remy Thanks for the response. I tried ClearResources and it didn't change anything. I also checked return value of Release and it is 0 as expected. – Mehrzad Chehraz Dec 13 '17 at 20:35
  • Short of a D2D driver bug, it sounds like maybe D2D is simply holding on to internal resources, probably caching them for later reuse. Not unheard of in memory management systems, especially when allocating new resources dynamically may be slow/expensive. Is there a reason why your tasks can't reuse devices? – Remy Lebeau Dec 13 '17 at 20:41
  • @RemyLebeau I can reuse them but since the entire design is OO, I don't like to hold a static reference and having Init, Shutdown pair functions in the program lifetime. However, check my update, looks like problem is somehow solved. – Mehrzad Chehraz Dec 13 '17 at 20:57

0 Answers0