zezba9000
zezba9000

Reputation: 3383

WinRT C++/CX Component ComPtr vs native Pointer

I'm looking at some Direct3D code for WinRT and notice in there "ref classes" there using "ComPtr<ID3D11Device1>" instead of "ID3D11Device1*". So my question is do you HAVE to use "ComPtr" in a "ref class" or can you use a "native pointer" instead??

Right now i'm using a native pointer in a ref class as my c++ files are also used to compile Managed C++ as well. But sometimes I get odd behavior in WinRT and think it might have something to do with ComPtr.

Upvotes: 3

Views: 3518

Answers (1)

nogard
nogard

Reputation: 9716

It is not necessary to use, but often very convenient.

It is reference counting smart pointer for COM objects that manages the lifetime of a COM object. If you use raw pointer you often can't guarantee that the COM object is still alive. If you think this could be related to your issue (e.g. you get access violation when calling COM methods) then it makes sense to use them. Performance penalty for reference counting is nothing compared to COM method invocation anyway.

Upvotes: 4

Related Questions