【问题标题】:Smart pointer with custom deleter as class member具有自定义删除器作为类成员的智能指针
【发布时间】: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 在复制后停止工作?

编辑:我部分找到了答案 -
FactoryFunctionCustomDeleterFunction 仅适用于 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


【解决方案1】:

我个人觉得这不是与自定义删除功能的智能指针有关的问题。 因为有一个称为返回值优化的概念,当函数按值返回 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

【讨论】:

  • 您好!使用我的 GCC 7.3.0 32 位代码输出为 11434840 1。第一个数字是完全随机的,因为我运行代码几次并且值总是不同。
猜你喜欢
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 2012-03-18
  • 2015-10-01
  • 2013-04-02
  • 2013-03-16
相关资源
最近更新 更多