Alex Rivera | Logout

Is it safe to use STL (TR1) shared_ptr's between modules (exes and dlls)

Asked 2008-12-05T20:21:04.080
16

I know that new-ing something in one module and delete-ing it in another can often cause problems in VC++. Problems with different runtimes. Mixing modules with staticly linked runtimes and/or dynamically linked versioning mismatches both can screw stuff up if I recall correctly.

However, is it safe to use VC++ 2008's std::tr1::shared_ptr across modules?

Since there is only one version of the runtime that even knows what what a shared_ptr is, static linking is my only danger (for now...). I thought I've read that boost's version of a shared_ptr was safe to use like this, but I'm using Redmond's version...

I'm trying to avoid having a special call to free objects in the allocating module. (or something like a "delete this" in the class itself). If this all seems a little hacky, I'm using this for unit testing. If you've ever tried to unit test existing C++ code you can understand how creative you need to be at times. My memory is allocated by an EXE, but ultimately will be freed in a DLL (if the reference counting works the way I think it does).

Edit
Report

1 Answer

2

I'd guess it is as safe as to use any of the classes in std across modules.

That is: It should be safe if the modules use exactly the same runtime library, and exactly the same compiler switches and options.

Never use the static runtime library, as each module will get their own instance of all globals within it.

answered 2008-12-05T20:47:56.943

Your Answer