在DLL接口使用的shared_ptr [英] Using shared_ptr in dll-interfaces

查看:1006
本文介绍了在DLL接口使用的shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,在我的DLL。

I have an abstract class in my dll.

class IBase {
  protected:
       virtual ~IBase() = 0;
  public:
       virtual void f() = 0;
};

我想获得 IBase的中加载DLL我的EXE文件。
第一种方法是创建下面的函数

I want to get IBase in my exe-file which loads dll. First way is to create following function

IBase * CreateInterface();

和添加虚拟函数发布() IBase的

第二种方法是创建另一个函数

Second way is to create another function

boost::shared_ptr<IBase> CreateInterface();

和没有发布()需要的功能。

的问题。

1)这是真的,析构函数和释放内存中调用的DLL(而不是在EXE文件)的第二个案例的?

1) Is it true that the destructor and memory deallocation is called in the dll (not in exe-file) in the second case?

2)不的第二个案例的工作好,如果执行文件和dll与不同的编译器(或不同的设置)编译的。

2) Does the second case work well if exe-file and dll was compiled with different compilers (or different settings).

推荐答案

一个回答你的第一个问题:在DLL中的虚析构函数被调用 - 关于它的位置的信息被嵌入到对象(在虚函数表)。在内存释放的情况下,这取决于如何纪律你的 I基准用户是。如果他们知道他们必须调用发布(),并认为异常可以绕过一个奇怪的方向控制流,合适的人将被使用。

An answer to your first question: The virtual destructor in your dll is called - the information about its location is embedded in your object (in the vtable). In the case of memory deallocation it depends how disciplined the users of your IBase are. If they know they have to call Release() and consider that exception can bypass the control flow in an surprising direction, the right one will be used.

但是,如果 CreateInterface()收益的shared_ptr&LT; IBase的&GT; 它可以正确的释放函数绑定权这个聪明指针。您的图书馆可能是这样的:

But if CreateInterface() returns shared_ptr<IBase> it can bind the right deallocation function right to this smart pointer. Your library may look like this:

Destroy(IBase* p) {
    ... // whatever is needed to delete your object in the right way    
}

boost::shared_ptr<IBase> CreateInterface() {
    IBase *p = new MyConcreteBase(...);
    ...
    return shared_ptr<IBase>(p, Destroy); // bind Destroy() to the shared_ptr
}                                         // which is called instead of a plain
                                          // delete

因此​​,你的DLL的每个用户很容易$ pvented对资源泄漏p $。他们从来没有理会调用发布()还是要注意异常出奇绕过其控制流程。

Thus every user of your DLL is easily prevented against resource leaks. They never have to bother about calling Release() or pay attention to exceptions bypassing surprisingly their control flow.

要回答你的第二个问题:此方法的缺点是明显地被其他<一说href=\"http://stackoverflow.com/questions/1605640/using-sharedptr-in-dll-interfaces/1605668#1605668\">answers:你是观众已经使用相同的编译器,连接器,设置,库,你。如果他们可以相当多这可以为您的图书馆主要缺点。你必须选择:安全与更多的观众。

To answer your second question: The downside of this approach is clearly stated by the other answers: You're audience has to use the same compiler, linker, settings, libraries as you. And if they could be quite a lot this can be major drawback for your library. You have to choose: Safety vs. larger audience

但有一个可能出现的漏洞:使用的shared_ptr&LT; IBase的&GT; 在应用程序中,即

But there's a possible loophole: Use shared_ptr<IBase>in your application, i.e.

{
    shared_ptr<IBase> p(CreateInterface(), DestroyFromLibrary);
    ...
    func();
    ...
}

因此​​,没有实施的具体对象跨边界的DLL传递。不过你的指针被安全地隐藏在的shared_ptr ,谁在呼叫 DestroyFromLibrary 在正确的时间,即使 FUNC()的抛出一个异常或没有。

Thus no implementation specific object is passed across the DLL boundary. Nevertheless your pointer is safely hidden behind the shared_ptr, who's calling DestroyFromLibrary at the right time even if func()'s throwing an exception or not.

这篇关于在DLL接口使用的shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆