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

后端 未结 5 1617
借酒劲吻你
借酒劲吻你 2020-12-28 19:14

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 lin

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-28 19:56

    The best advice I've seen on the general subject is that memory should be deallocated in the same context it's allocated. That doesn't preclude a library passing back a pointer that application code is supposed to free however, so I'd say you're probably safe passing the shared_ptr in this manner, as it's the same general situation.

    If the semantics of your system mean that the pointer is actually transferred (in the ownership sense) from your exe to your dll, then an auto_ptr might be a better solution. If, however, your pointer is truly shared, then the shared_ptr is probably the best solution.

提交回复
热议问题