在DLL接口中使用boost :: shared ptr可以吗? [英] Is it OK to use boost::shared ptr in DLL interface?

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

问题描述



是否可以导出这样的函数?

是否有效开发一个C ++中的DLL,它返回boost共享指针并使用它们作为参数? / p>

  1。)boost :: shared_ptr< Connection> startConnection(); 
2.)void sendToConnection(boost :: shared_ptr< Connection> conn,byte * data,int len);

在特殊情况下:引用计数是否跨越DLL边界工作,或者要求是exe和dll使用相同的运行时间?



目的是克服对象所有权的问题。因此,当dll和exe都不再引用它时,对象被删除。

解决方案

根据Scott Meyers在Effective C ++ (第3版),shared_ptrs在dll边界是安全的。 shared_ptr对象从创建它的DLL中保存一个指向析构函数的指针。


在第18项的书中,他指出:
tr1 :: shared_ptr的好功能是它自动使用其每个指针删除
来消除另一个潜在的客户端错误,跨DLL问题。
当一个对象是使用新的
动态链接库(DLL)创建的,但在不同的DLL中被删除。在
许多平台上,这样的跨DLL新/删除对导致运行时
错误。 tr1 :: shared_ptr避免了这个问题,因为它的缺省删除器
使用了创建tr1 :: shared_ptr的同一个DLL中的删除。


Tim Lesher有一个有趣的getcha,但是,他提到此处。您需要确保在shared_ptr最终超出范围之前,创建shared_ptr的DLL不会被卸载。我会说,在大多数情况下,这不是你必须注意的事情,但是如果你正在创建将被松散耦合的dll,那么我建议不要使用shared_ptr。



另一个潜在的缺点是确保双方都是使用boost库的兼容版本创建的。 Boost的shared_ptr已经稳定了很久。至少自从 1.34 以来,已经被tr1兼容了。 / p>

Is it valid to develop a DLL in C++ that returns boost shared pointers and uses them as parameters?

So, is it ok to export functions like this?

1.) boost::shared_ptr<Connection> startConnection();
2.) void sendToConnection(boost::shared_ptr<Connection> conn, byte* data, int len);

In special: Does the reference count work across DLL boundaries or would the requirement be that exe and dll use the same runtime?

The intention is to overcome the problems with object ownership. So the object gets deleted when both dll and exe don't reference it any more.

解决方案

According to Scott Meyers in Effective C++ (3rd Edition), shared_ptrs are safe across dll boundaries. The shared_ptr object keeps a pointer to the destructor from the dll that created it.

In his book in Item 18 he states, "An especially nice feature of tr1::shared_ptr is that it automatically uses its per-pointer deleter to eliminate another potential client error, the "cross-DLL problem." This problem crops up when an object is created using new in one dynamically linked library (DLL) but is deleted in a different DLL. On many platforms, such cross-DLL new/delete pairs lead to runtime errors. tr1::shared_ptr avoid the problem, because its default deleter uses delete from the same DLL where the tr1::shared_ptr is created."

Tim Lesher has an interesting gotcha to watch for, though, that he mentions here. You need to make sure that the DLL that created the shared_ptr isn't unloaded before the shared_ptr finally goes out of scope. I would say that in most cases this isn't something you have to watch for, but if you're creating dlls that will be loosely coupled then I would recommend against using a shared_ptr.

Another potential downside is making sure both sides are created with compatible versions of the boost library. Boost's shared_ptr has been stable for a long while. At least since 1.34 it's been tr1 compatible.

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

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