gpt4 book ai didi

c++ - 具有自定义删除器的智能指针作为类成员

转载 作者:行者123 更新时间:2023-12-01 14:57:58 27 4
gpt4 key购买 nike

我的情况如下:
我有一个.dll文件和一个带有纯虚拟class MainWindow声明的.h header 。头文件中有两个类型定义:

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库加载函数也成功。 header 中定义的 MainWindow具有绘制用户界面的 Show()方法。如果我尝试使用 ShowWindow()中的 class Loader方法像上面一样调用它,则不会出现该窗口。
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函数范围内起作用。我不知道为什么。

最佳答案

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

关于c++ - 具有自定义删除器的智能指针作为类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60563820/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com