C++11 正确使用共享ptr进行显式库链接
我正在使用and(以及其他一些博客、帖子等,但我的声誉太低,无法发布它们)尝试创建一个可以显式链接到的类库。我想做的是使用std::shared_ptr在类终止时(或者它不在范围内)处理类的基本删除。我的代码可以编译,但在运行时,它在xrefwrap中不断遇到访问冲突。下面是我的单元测试代码:C++11 正确使用共享ptr进行显式库链接,c++11,shared-ptr,C++11,Shared Ptr,我正在使用and(以及其他一些博客、帖子等,但我的声誉太低,无法发布它们)尝试创建一个可以显式链接到的类库。我想做的是使用std::shared_ptr在类终止时(或者它不在范围内)处理类的基本删除。我的代码可以编译,但在运行时,它在xrefwrap中不断遇到访问冲突。下面是我的单元测试代码: typedef IHermes* (*pHermesFactory)(); typedef std::shared_ptr<IHermes> IHermesPtr; using namesp
typedef IHermes* (*pHermesFactory)();
typedef std::shared_ptr<IHermes> IHermesPtr;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int iReturnValue = 0;
HINSTANCE hInstance = LoadLibrary(TEXT("Hermes.dll"));
assert(hInstance);
pHermesFactory pHermes = (pHermesFactory)GetProcAddress(hInstance, "HermesFactory");
assert(pHermes);
IHermesPtr ptrHermes(pHermes(), std::mem_fn(&IHermes::Release));
assert(ptrHermes);
FINISH:
if (hInstance)
FreeLibrary(hInstance);<br/>
return iReturnValue;
}
最后,工厂的功能是:
(extern "c" - declared in header not in the implementation) __cdecl(dllexport) IHermes* APIENTRY HermesFactory(){ return new Hermes; }
以下是此内容违反访问权限的地方(来自xrefwrap):
模板
静态重新调用pmf(\u pmf\u Pm,\u Farg0&&u Fx0,假类型,\u Ftypes&&u Fargs)
{//应用于(可能是智能)指针
返回((*_Fx0)。*_Pm)(_标准前进(_Fargs)…)freebrary
卸载库,然后当main
返回shared_ptr
时,析构函数运行并调用其deleter,从而调用IHermes::Release
,这可能是库以前所在的位置。接下来会发生很多有趣的事情。除非您正在积极移植古老的windows,否则不要使用TCHAR
凯西,你有一个。你在天空中看到的红光不是鲁道夫;不是,是因为我错过了这么简单的东西而感到尴尬。
(extern "c" - declared in header not in the implementation) __cdecl(dllexport) IHermes* APIENTRY HermesFactory(){ return new Hermes; }
template<class _Pmf,
class _Farg0,
class... _Ftypes>
static _Ret _Call_pmf(_Pmf _Pm, _Farg0&& _Fx0, false_type, _Ftypes&&... _Fargs)
{ // apply to (possibly smart) pointer
return (((*_Fx0).*_Pm)(_STD forward<_Ftypes>(_Fargs)...)); <-- ACCESS VIOLATION??
}