Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a custom deleter for std::shared_ptr on a direct3d11 object

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 :\

like image 600
FatalCatharsis Avatar asked Nov 29 '12 20:11

FatalCatharsis


2 Answers

How about this?

std::shared_ptr<ID3D11Device> ptr(nullptr, [](ID3D11Device* ptr){ptr->Release();} ); 
like image 181
Arne Mertz Avatar answered Sep 19 '22 12:09

Arne Mertz


Try This

struct Releaser{
    void operator()(ID3D11Device* p){
        p->Release();

    };

};


std::shared_ptr<ID3D11Device> ptr(nullptr, Releaser());
like image 35
John Bandela Avatar answered Sep 19 '22 12:09

John Bandela