在DLL接口中使用boost :: shared ptr可以吗?

在C中开发一个DLL可以返回升级共享指针并将其用作参数是否有效?

那么输出这样的功能可以吗?

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

特别的:引用计数是否跨DLL的边界工作,或者是exe和dll使用相同的运行时间?

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

根据有效C(第3版)的Scott Meyers的说法,shared_ptrs在dll边界上是安全的. shared_ptr对象从创建它的dll中保留一个指向析构函数的指针.

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有一个有趣的呵呵,但是他提到了here.你需要确保在shared_ptr最终超出范围之前创建shared_ptr的DLL不会被卸载.我会说,在大多数情况下,这不是你必须注意的事情,但如果你正在创建一个松散耦合的dll,那么我建议不要使用shared_ptr.

一个潜在的缺点是确保双方都是使用兼容版本的boost库创建的. Boost的shared_ptr已经稳定了很久.至少从1.34起,它已经被tr1兼容了.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...