0

I am writing a simulation project in C++ using Visual Studio 2012 (standard C++ with no /clr support) and I face the following problem. In the code somewhere I pass a string by reference:

theClassObject->Method("12345") 

where the definition of the method in the class is:

TheClass::Method(const std::string& STR) {..} 

and when I check the value of the STR during debugging it has four more bytes in front, for example "~#.a12345" and the length of the string is increased by these four bytes. In other places of the code with other similar methods this does not happen.

Did I miss some compiler option for the project or there are other reasons I miss?

I am not sure if I provided all the information needed, but I could if needed.

Thanks in advance!

4
  • 2
    This should not happen. From what you posted, it's pretty much impossible to guess what went wrong. Can you reduce your problem to a subset that is still compilable and post that? Commented Feb 7, 2014 at 10:35
  • You are sending it as const reference, so you cannot change the string inside the function Method() , It is being changed somewhere else. Can you post a sample working code?(SSCCE) Commented Feb 7, 2014 at 10:37
  • It is strange... you are altering a const reference! Post the implementation of TheClass::Method(const std::string& STR). It is probably a debug configuration which is showing a wrong value. Commented Feb 7, 2014 at 10:39
  • Sounds like memory corruption, run the program through a memory checker (i.e. valgrind on linux). Commented Feb 7, 2014 at 11:17

1 Answer 1

2

This quacks loudly like a module boundary problem, calling code in another DLL that was built with an incompatible version of std::string or incompatible compiler settings. Like _HAS_ITERATOR_DEBUGGING.

The only fix for it is recompiling everything with the exact same compiler version, C++ library and build settings.

Exposing C++ objects across module boundaries is a perilous adventure. Especially the ones in the std namespace and especially as of late with C++11 bringing big changes to the standard library implementation. Truly trouble-free DLL interfaces requires pretty draconian measures, the kind that look a lot like COM.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.