Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually release ComPtr

Tags:

c++

com

I'm using ComPtr (Microsoft::WRL) to manage some DirectX11 resources. How can I manually release it?

The "ReleaseAndGetAddressOf" method if I understand correctly, only frees the pointer and not the resource itself (which is returned), and I'm not sure about the "Reset" method.

The only alternatives I could think of are manually calling the pointer destructor, or after obtaining the raw pointer from "ReleaseAndGetAddressOf" calling "Release" on that, which I would like to avoid.

like image 873
キキジキ Avatar asked Nov 16 '12 14:11

キキジキ


2 Answers

The source code for WRL is provided, have a look at include/winrt/wrl/client.h. The embedded COM pointer (ptr_ member) is released by the InternalRelease() function. Making any of the following a way to release the pointer suitable candidates:

  • the destructor. The reason to use ComPtr<>
  • assigning nullptr
  • using ReleaseAndGetAddressOf(), the long way around
  • calling Reset()

So assigning nullptr or calling Reset() are a good fit, take your pick. Or don't use it at all if you just want to manage the interface pointer yourself, it certainly isn't required to use ComPtr.

like image 150
Hans Passant Avatar answered Nov 08 '22 20:11

Hans Passant


You can assign a null pointer.

like image 41
Simon Richter Avatar answered Nov 08 '22 22:11

Simon Richter