Why does this program crash: passing of std::string between DLLs
Posted
by msiemeri
on Stack Overflow
See other posts from Stack Overflow
or by msiemeri
Published on 2010-02-23T22:01:53Z
Indexed on
2010/04/09
15:23 UTC
Read the original article
Hit count: 336
Hello together.
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 A {
__declspec(dllexport) std::string getString();
};
//A.cpp
#include "A.h"
std::string A::getString() {
return "I am a string.";
}
//// the following compiles to main.exe with debug runtime linked dynamically
#include "A.h"
int main() {
A a;
std::string s = A.getString();
return 0;
} // crash on exit
Obviously (?) this is due to the different memory models for the executable and DLL. Could it be that the string A::getString()
returns is being allocated in A.dll and freed in main.exe?
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.
© Stack Overflow or respective owner