开发者

using shared_ptr<T> across libs with different CRTs

I am writing a library that includes an interface to return\receive shared_ptr objects.

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.

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? (STL doesn't provide bi开发者_如何学Pythonnary compatibility)

  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. since this is a template class, and CRT versions differ, how can I make sure a new has the matching 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. 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.

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?

Thanks for any help, Leo


AFAIK you cannot easily do this, though you could conceivably create a binary-compatible shared smart pointer class, e.g. on Microsoft platforms using COM objects. You still have to make sure that allocation and deallocation both happen in the same 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜