1.) boost::shared_ptr<Connection> startConnection(); 2.) void sendToConnection(boost::shared_ptr<Connection> conn,byte* data,int len);
特别的:引用计数是否跨DLL的边界工作,或者是exe和dll使用相同的运行时间?
目的是克服对象所有权问题.所以当dll和exe不再引用它时,对象被删除.
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兼容了.