简体   繁体   English

使用 shared_ptr<t> 跨具有不同 CRT 的库</t>

[英]using shared_ptr<T> across libs with different CRTs

I am writing a library that includes an interface to return\receive shared_ptr objects.我正在编写一个库,其中包含一个返回\接收shared_ptr对象的接口。

Everything seemed just dandy until I was reminded that an application using my library could have a different CRT version as well as STL version.一切似乎都很花哨,直到我被提醒使用我的库的应用程序可能有不同的 CRT 版本以及 STL 版本。

this begs two questions:这引出了两个问题:

  1. if STL versions differ between my.lib and the app using it, how can I make sure the same version of shared_ptr is being used across my lib and app?如果 my.lib 和使用它的应用程序之间的 STL 版本不同,我如何确保在我的 lib 和应用程序中使用相同版本的shared_ptr (STL doesn't provide binary compatibility) (STL 不提供二进制兼容性)

  2. let's say I solved question 1 by copying STL's header defining shared_ptr and renaming it to my_shared_ptr or in any way that now gives me the same shared_ptr implementation across my lib and the app.假设我通过复制 STL 的 header 定义shared_ptr并将其重命名为my_shared_ptr或以现在在我的 lib 和应用程序中为我提供相同shared_ptr实现的任何方式解决了问题 1。 since this is a template class, and CRT versions differ, how can I make sure a new has the matching delete ?由于这是一个模板 class,并且 CRT 版本不同,我怎样才能确保一个new的有匹配的delete If my lib compiles my_shared_ptr<SomeClass> and the app compiles my_shared_ptr<SomeClass> , each compilation sticks in its own new and delete according to the CRT version.如果我的 lib 编译my_shared_ptr<SomeClass>并且应用程序编译my_shared_ptr<SomeClass> ,则每个编译都会根据 CRT 版本保留自己的newdelete Now when I create and hand a shared_ptr from the app to my lib to destroy when done with it, mismatching new and delete might be called.现在,当我从应用程序创建一个shared_ptr并将其交给我的 lib 以销毁它时,可能会调用不匹配newdelete

Am I right to assume two instantiations of my_shared_ptr<SomeClass> are to be compiled, one by the.lib and one by the app?我是否正确假设要编译my_shared_ptr<SomeClass>的两个实例,一个由 the.lib 编译,一个由应用程序编译?

Thanks for any help, Leo谢谢你的帮助,狮子座

AFAIK you cannot easily do this, though you could conceivably create a binary-compatible shared smart pointer class, eg on Microsoft platforms using COM objects. AFAIK 你不能轻易做到这一点,尽管你可以想象创建一个二进制兼容的共享智能指针 class,例如在 Microsoft 平台上使用 COM 对象。 You still have to make sure that allocation and deallocation both happen in the same DLL.您仍然必须确保分配和解除分配都发生在同一个 DLL 中。 This will mean replacing "new" and "delete" of your smart pointer class will calls into functions exported from your DLL that are guaranteed to call the correct new and delete implementations.这将意味着替换智能指针 class 的“新建”和“删除”将调用从 DLL 导出的函数,这些函数保证调用正确的新建和删除实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM