返回从本机 C++ 调用编组的 System::String 时崩溃

Crash on returning a System::String that was marshalled from a native C++ call

我有一个本机 C++ dll,我从托管的 C++ dll 调用它,它从 C# 应用程序调用。

本机 dll 中的函数如下所示:

std::string NativeClass::Test()
{
    return std::string("some string");
}

调用它的托管 C++ 函数看起来像这样

String ^ ManagedClass::Test()
{
    std::string temp = this->_native->Test();
    String^ sRet = msclr::interop::marshal_as<String^>(temp);

    return sRet; // crashes here !!!
}

但是,在执行 return 语句时,应用程序崩溃并出现类似

的错误
Debug Assertion Failed!
debug_heap.cpp
Line 980
Expression: __acrt_first_block == header

我查看了整个 Whosebug,但我还没有解决它。有什么想法吗?

汉斯帮助解决了这个问题。确实是本机 dll 和托管 dll 中的 CRT 版本不匹配。我确定我已经检查过了,但显然错过了。代码否则按原样工作。谢谢。