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

 ̄綄美尐妖づ 提交于 2019-12-06 02:52:11

问题


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


回答1:


How about this?

std::shared_ptr<ID3D11Device> ptr(nullptr, [](ID3D11Device* ptr){ptr->Release();} ); 



回答2:


Try This

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

    };

};


std::shared_ptr<ID3D11Device> ptr(nullptr, Releaser());


来源:https://stackoverflow.com/questions/13633737/using-a-custom-deleter-for-stdshared-ptr-on-a-direct3d11-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!