Failed to free Direct3D device "or" Relevant context

Yes, its actually an "or . " I will explain. I develop helper classes for myself, for example DirectXToolKit. To control COM, I use Microsoft :: WRL :: ComPtr <T> (wrl.h).

struct Renderer { ComPtr<ID3D11Device> m_Device; ComPtr<ID3D11DeviceContext> m_ImmContext; } 

When all resources are destroyed, an instance of the above structure must also be destroyed, but after calling dtor, I run an error in Microsoft :: WRL :: ComPtr <T> when it tries to free the device or context.

I have implemented dtor, where I manually release m_Device and m_ImmContext, but unfortunately the last element I am trying to release always encounters a problem in the function

 unsigned long InternalRelease() throw() { unsigned long ref = 0; T* temp = ptr_; if (temp != nullptr) { ptr_ = nullptr; ref = temp->Release(); } return ref; } 

here

 ref = temp->Release(); 

When I manage to release the device first, the context causes an error, and vice versa (yes, when one of them was successfully released, the failure of the second participant failed). There was already a question like mine ( destroy directx device and swap chain ), but the window and swapchain were already destroyed, like other dx resources. I don’t know why this is happening. Any ideas?

Sorry for my imperfect English: 3

+4
source share
2 answers

I fixed this problem. The problem was that I did not understand std :: shared_ptr quite well (the std :: make_shared function actually ( about shared pointers and their distribution )):

I created a pointer like:

 Obj *ObjPtr = new Obj(); 

and then just:

 SomeOtherState.SharedObj = std::make_shared<Obj>(*ObjPtr); 

and did not destroy ObjPtr . ObjPtr data indicated that it was still in memory after SomeOtherState was destroyed (and as far as I understood, the problem should have gone if I used std :: shared_ptr ctor).

Most likely due to this memory leak, but another thing happened that I changed: the CoUninitialize call was made before the last COM pointer was destroyed, but from MSDN: CoUninitialize came to the point that it is important (from the first paragraph actually):

Closes the COM library in the current thread, unloads all the DLLs loaded by the thread, frees up any other resources that the thread supports, and causes all RPC connections in the thread to close.

So, I replaced CoInitialize and CoUninitialize , and the problem disappeared. If someone had the same problems, this could be a solution (either either the first or second change I made).

I should probably add com tags for my question

+3
source

The problem MAY be somewhere when you get the ImmediateContext. According to the documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/ff476529%28v=vs.85%29.aspx

 The GetImmediateContext method increments the reference count of the immediate context by one. Therefore, you must call Release on the returned interface pointer when you are done with it to avoid a memory leak. 

So, I β†’> guess <<<that you forgot to free the context somewhere, after which the device will crash.

BTW: make sure resources are ALWAYS released in reverse order.

0
source

All Articles