Why does this program crash: passing of std::string between DLLs

前端 未结 6 1711
名媛妹妹
名媛妹妹 2020-11-29 02:28

I have some trouble figuring out why the following crashes (MSVC9):

//// the following compiles to A.dll with release runtime linked dynamically
//A.h
class          


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 02:57

    Could it be that the string A::getString() returns is being allocated in A.dll and freed in main.exe?

    Yes.

    If so, why - and what would be a safe way to pass strings between DLLs (or executables, for that matter)? Without using wrappers like shared_ptr with a custom deleter.

    Using a shared_ptr sounds like a sensible thing to me. Remember, as a rule of thumb, allocations and deallocations should be done by the same module to avoid glitches like these.

    Exporting STL objects across dlls is at best a tricky pony. I suggest you check out this MSDN KB article first and this post.

提交回复
热议问题