When i use an std::shared_ptr and need a custom deleter, i usually make a member function of the object to facilitate it's destruction like this:
class Example
{
public:
Destroy();
};
and then when i use the shared ptr, i just make it like this:
std::shared_ptr<Example> ptr(new Example, std::mem_fun(&Example::Destroy));
Problem is, right now i'm working with d3d11, and i would like to use the com release functions as an std::shared_ptr custom deleter, like this
std::shared_ptr<ID3D11Device> ptr(nullptr, std::mem_fun(&ID3D11Device::Release));
but i get this error:
error C2784: 'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Result (__thiscall _Ty::* )(_Arg) const' from 'ULONG (__stdcall IUnknown::* )(void)'
and then when i explicitly specify the template paramters like this:
std::shared_ptr<ID3D11Device> ptr(nullptr, std::mem_fun<ULONG, ID3D11Device>(&ID3D11Device::Release));
I get this error,
error C2665: 'std::mem_fun' : none of the 2 overloads could convert all the argument types
can someone explain why i can't use this function as a deleter?
note: don't suggest I use CComPtr, i'm using msvc++ express edition :\
How about this?
std::shared_ptr<ID3D11Device> ptr(nullptr, [](ID3D11Device* ptr){ptr->Release();} );
Try This
struct Releaser{
void operator()(ID3D11Device* p){
p->Release();
};
};
std::shared_ptr<ID3D11Device> ptr(nullptr, Releaser());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With