Freefly
Freefly

Reputation: 239

std::string gets corrupted/change structure when passed to a library

I am using Visual Studio 2012 for a project and a build I made myself of Log4cxx for logging purposes. Both projects are built in Debug x64 and (I believe) linked properly.

Platform toolset for both projects is Visual Studio 2012 (v110), Runtime Library is Multi-threaded Debug DLL (/MDd), Log4cxx is created and linked as a dynamic library (.dll).

Consider the following:

std::string szTestString = "Hello World!";
DOMConfigurator::configure(szTestString); // Call to Log4cxx.dll

While debugging, just before getting in that function, this is the value of szTestString:

String on my project's side

So far so good. Now I step into the function:

void DOMConfigurator::configure(const std::string& filename)
{
    File file(filename);
    DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository());
}

And when looking at filename...

enter image description here

That's obviously not right.

If I step once forward, I get an error:

Unhandled exception at at 0x000007FEFD6FBDED in SampleProject.exe: Microsoft C++ exception: std::length_error at memory location 0x000000000030F110.

What could be causing the change in std::string interpretation?

EDIT: currently I added a wrapper to Log4cxx to take char* instead of std::string and it did the trick. The question still stands however.

Upvotes: 3

Views: 1502

Answers (1)

Bharadwaj Gali
Bharadwaj Gali

Reputation: 103

This problem generally occurs when library is compiled in "Release" mode and you are trying to run application in "Debug" mode.

Keep both the components i.e., DLL and Application in same mode (Either Release or Debug).
Make sure that "Code Generation" property value for both DLL and Application are same.

Upvotes: 1

Related Questions