1
votes

I've recently been going through the process of replacing all my raw pointers with c++11 smart pointers, and now I'm finally done with the exception of my DirectX long pointers.

I'd like to get something along the lines of the below implemented:

std::shared_ptr<IDirect3D9> p_d3d;
p_d3d( Direct3DCreate9(D3D_SDK_VERSION), [](IDirect3D9 *p) {//smart pointer initialization(1)
        p->Release();
});

I'm getting the following error from visual studio:

error : call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type

I'd prefer to use std::unique_ptr or std::shared_ptr than CComPtr. Any ideas on why this is happening and possible methods of implementation?

1
The initialization is done when you declare the variable.chris
The C++ smart pointers do not support the ref-counting semantics that COM objects rely on. You are well advised using the COM smart pointers.IInspectable
having two competing reference counting schemes is not a good idea. in particular with your intended scheme the referred to object can be destroyed while you have a set of smart pointers to it. instead build your own COM smart pointer. if you're using the Boost library you can save some work by using boost::intrusive_ptr (if I recall its name correctly). unfortunately that building block was not adopted into the C++11 standard.Cheers and hth. - Alf
@cheers I do not see how myself. The set of all shared_ptr can share one com reference count. When they all go away, the last one releases it. There is no more problem here than having a shared_ptr owned object that has a CComPtr member variable.Yakk - Adam Nevraumont
@Yakk: it's no problem calling p->Release() when p is a std::shared_ptr point to a COM object. Then you still have a pointer but no pointee. As I recall the ATL COM pointers\ fixes this by having operator-> returning the pointer downcasted to an interface where AddRef and Release are private. Anyway that's how I do it. In addition to security, you also want your COM smart pointer to support certain COM things, such as casting to an interface.Cheers and hth. - Alf

1 Answers

2
votes

You seem to be calling operator() on your shared_ptr. That is not how you construct a shared_ptr. Try using reset, or constructing the pointer on the same line as you declare the variable.

Be careful that the creation function you call gives you a pointer with a reference count of one.