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:
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)let's say I solved question 1 by copying STL's header defining
shared_ptr
and renaming it tomy_shared_ptr
or in any way that now gives me the sameshared_ptr
implementation across my lib and the app. since this is a template class, and CRT versions differ, how can I make sure anew
has the matchingdelete
? If my lib compilesmy_shared_ptr<SomeClass>
and the app compilesmy_shared_ptr<SomeClass>
, each compilation sticks in its ownnew
anddelete
according to the CRT version. Now when I create and hand ashared_ptr
from the app to my lib to destroy when done with it, mismatchingnew
anddelete
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.
精彩评论