Do I completely deny the benefits of Microsoft :: WRL :: ComPtr by passing it as a link (&)?

I built ComPtrs in my code because I need them here and there, but I did it like this:

HRESULT Material::Initialize(aiMaterial* pMaterial, Microsoft::WRL::ComPtr<ID3D11Device1> & d3dDevice, Microsoft::WRL::ComPtr<ID3D11DeviceContext1> & d3dContext) 

Does this completely deny the utility of ComPtr counting? Should I just skip over the value (no &) instead?

thank you for reading

+4
source share
3 answers

Absolutely normal and prefers to pass it as const &.

Passing by value is acceptable in terms of semantics, not so much from performance, as passing leads to the fact that overflow of the refcount occurs up and down, and both are "interconnected" operations with serious consequences. And we get nothing in return.

The advantage of ComPtr is that it allows you to properly match Release calls that are too easy to mess up with, and even if it was easy, the mess of the code it takes is unpleasant.

+2
source

No, you are doing the right thing. The agent should not change the reference counter if he does not need to hold the interface for access after the call. Increasing and decreasing the reference count is not free - it's a virtual call plus a blocked increment or decrement - so you get better performance anyway. You should use a constant reference, though, or just simply pass a raw pointer.

+1
source

In my DirectX code, I install my engine to find out which of the objects has the right to control the lifetime of the DirectX COM object. Then I pass it as a raw pointer to the methods that need them (I avoid having member variables keep track of DX objects as much as possible).

+1
source

Source: https://habr.com/ru/post/1486686/


All Articles