【发布时间】:2020-06-19 04:11:30
【问题描述】:
我的情况如下:
我有一个 .dll 文件和一个带有纯虚拟 class MainWindow 声明的 .h 标头。头文件中有两种类型定义:
typedef MainWindow* (*CREATE_INTERFACE)();
typedef void (*DELETE_INTERFACE)(MainWindow* Window);
它允许我创建和删除与虚拟类相同类型的对象。
我希望在我的类中定义对这个动态库的加载支持,我们称之为class Loader。我希望我的班级成员之一是定义如下的智能指针:
std::unique_ptr <MainWindow,DELETE_INTERFACE> UI_Driver;
下面的简化代码(没有错误处理):
class Loader
{
private:
HINSTANCE DllHandle;
CREATE_INTERFACE FactoryFunction;
DELETE_INTERFACE CustomDeleterFunction;
std::unique_ptr<MainWindow,DELETE_INTERFACE> UI_Driver;
std::unique_ptr<MainWindow,DELETE_INTERFACE> LoadExternalLibrary()
{
std::wstring WideFileName=L"MainWindow.dll";
std::string FileName(std::begin(WideFileName),std::end(WideFileName));
this->DllHandle=::LoadLibrary(WideFileName.c_str());
// Get the function from the DLL
FactoryFunction=reinterpret_cast<CREATE_INTERFACE>(::GetProcAddress(DllHandle,"Create"));
CustomDeleterFunction=(DELETE_INTERFACE)GetProcAddress(DllHandle,"Delete");
return std::unique_ptr<MainWindow,DELETE_INTERFACE>(FactoryFunction(),CustomDeleterFunction);
};
public:
UI_Service() : UI_Driver(LoadExternalLibrary())
{
}
~UI_Service()
{
if(UI_Driver)
::FreeLibrary(this->DllHandle);
}
void ShowWindow()
{
UI_Driver->Show();
}
};
代码编译正确,从 .dll 库加载函数也成功。标题中定义的MainWindow 有一个绘制用户界面的Show() 方法。如果我尝试使用我的class Loader 中的ShowWindow() 方法像上面那样调用它,则不会出现该窗口。
int main()
{
Loader MyLoader;
MyLoader.ShowWindow(); // <- window does not appear, program crashes
}
但是,如果我在 LoadExternalLibrary() 方法中创建指针后立即调用此方法,如下所示:
所以改为:
return std::unique_ptr<Eol_MainWindow_Driver_Generic,DELETE_INTERFACE>(FactoryFunction(),CustomDeleterFunction);
我会写:
std::unique_ptr<Eol_MainWindow_Driver_Generic,DELETE_INTERFACE> UI(FactoryFunction(),CustomDeleterFunction);
UI->Show(); // <- Window appears
return UI; // <- It will never happen because the Show() is blocking the thread
发生了什么事?为什么使用自定义删除器创建的 unique_ptr 在复制后停止工作?
编辑:我部分找到了答案 -FactoryFunction 和 CustomDeleterFunction 仅适用于 LoadExternalDll 函数范围。我不知道为什么。
【问题讨论】:
-
使用您提供的简化代码还是仅使用完整代码可以重现问题?
-
因为您使用的是
c++11,所以我猜测自定义删除器在移动后会在您按值返回时使用nullptr调用。你看过删除器的实现吗?用nullptr调用它会破坏什么吗? -
@RinatVeliakhmedov 简化代码的结果完全一样。
-
@super extern "C" __declspec(dllexport) void __stdcall Delete(MainWindow* Driver) { std::cout
-
@super deleter 如果被调用会打印到标准输出——这里不会发生。
标签: c++ c++11 dll smart-pointers