将自定义删除程序作为类成员的智能指针



我的情况如下:我有一个.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

发生了什么事?为什么创建的带有自定义deleter的unique_ptr在复制后会停止工作?

EDIT:我找到了部分答案-
FactoryFunctionCustomDeleterFunction仅在LoadExternalDll函数范围内工作。我不知道为什么。

我个人认为这不是与具有自定义删除功能的智能指针相关的问题。因为有一个称为返回值优化的概念,当函数按值返回std::unique_ptr而不发生复制/移动时,就会发生这种情况。你可以在这里阅读更多Copy_elision

为了说明这一点,我创建了一个具有自定义删除功能的std::unique_ptr的小示例。

#include <iostream>
#include <memory>
#include <vector>
using customDeleteType = void(*)(std::vector<int*>*);
class Demo{
public:
    Demo();
    ~Demo();
    void printVector() const;
private:
    std::unique_ptr<std::vector<int*>, customDeleteType> populateVector() const;
    std::unique_ptr<std::vector<int*>, customDeleteType> vec;
};
Demo::Demo(): vec(populateVector()){
}
Demo::~Demo(){
}
void Demo::printVector() const{
    for(int* i: *vec){
        std::cout<< *i<< " ";
    }
    std::cout<< 'n';
}
std::unique_ptr<std::vector<int*>, customDeleteType> Demo::populateVector() const
{
    return std::unique_ptr<std::vector<int*>, customDeleteType>(
                new std::vector<int*>{new int{0}, new int{1}},
                [](std::vector<int*>* v){ for(int* i: *v){delete i;} delete v;}
                );
}
int main(int , char *[]){
    Demo d;
    d.printVector();
}

输出:0 1

最新更新