简体   繁体   English

std::string 在传递给库时被破坏/改变结构

[英]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.我将 Visual Studio 2012 用于一个项目和一个我自己制作的 Log4cxx 用于日志记录目的。 Both projects are built in Debug x64 and (I believe) linked properly.这两个项目都是在 Debug x64 中构建的,并且(我相信)链接正确。

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).这两个项目的平台工具集是 Visual Studio 2012 (v110),运行时库是多线程调试 DLL (/MDd),Log4cxx 是作为动态库 (.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:在调试时,就在进入该函数之前,这是 szTestString 的值:

我的项目方面的字符串

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...当查看文件名时......

在此处输入图片说明

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. SampleProject.exe 中 0x000007FEFD6FBDED 处的未处理异常:Microsoft C++ 异常:内存位置 0x000000000030F110 处的 std::length_error。

What could be causing the change in std::string interpretation?什么可能导致 std::string 解释发生变化?

EDIT: currently I added a wrapper to Log4cxx to take char* instead of std::string and it did the trick.编辑:目前我向 Log4cxx 添加了一个包装器以获取 char* 而不是 std::string 并且它做到了。 The question still stands however.然而,这个问题仍然存在。

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 ie, DLL and Application in same mode (Either Release or Debug).保持两个组件,即 DLL 和应用程序处于相同模式(发布或调试)。
Make sure that "Code Generation" property value for both DLL and Application are same.确保 DLL 和应用程序的“代码生成”属性值相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM