Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 768
Alex Millette Avatar asked Mar 07 '26 08:03

Alex Millette


1 Answers

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.

like image 80
Bharadwaj Gali Avatar answered Mar 08 '26 22:03

Bharadwaj Gali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!