Visual C++ Release build - is string getting corrupted when passed across DLL because compiled with different runtime version?
corruption, dll, visual-c++
Solution
If you pass non-POD (plain old datatypes) between DLL/EXE boundaries (like STL string or CRT FILE pointers) you must use the same shared CRT.
In your case, you must recompile all DLLs/LIBs with the same compiler!
See also: I can pass std::string for a Dll and what i can do with DLL´s?
Problem
After building in Release mode, I am seeing exceptions which didn't occur in Debug mode. When debugging the release build, it looks like string references are not being passed correctly from the EXE (our application) to the DLL which is receiving the string reference. Our EXE code looks like this: ``` string contents = "handle_message(): received=" + msg->encode(); LOG4CXX_DEBUG(logger, contents); ``` The `LOG4CXX_DEBUG` is going to `log4cxx.dll`, whose code looks like this: ``` CharMessageBuffer& CharMessageBuffer::operator<<(const std::basic_string<char>& msg) { if (stream == 0) { buf.append(msg); } else { *stream << msg; } return *this; } ``` Looking at the Call Stack in the debugger, when I navigate down to the frame which has our source, I can see that `contents` is a valid string with `size=583, capacity=838`. In the frame inside the `log4cxx.dll` (the next frame above in the stack) the string reference shows `size=838, capacity=363113231` (and the values are all garbage). Both our app and log4cxx.dll were compiled on the same machine, using the same runtime settings (/MD), but different versions of Visual Studio. The log4cxx dll was compiled using Visual Studio 2008 and our application was compiled using Visual Studio 2010. Running dumpbin on the 2 objects shows: Our App (EXE) ``` MSVCP100.dll MSVCR100.dll ``` log4cxx.dll (DLL) ``` MSVCP90.dll MSVCR90.dll ``` Is this problem due to the fact that they are using different runtime versions?