调试断言失败错误时,将std :: string从c ++ / cli dll传递给本机win32 dll [英] Debug assertion failed error when passing std::string from c++/cli dll to native win32 dll

查看:464
本文介绍了调试断言失败错误时,将std :: string从c ++ / cli dll传递给本机win32 dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#窗体调用c ++ / cli接口DLL,它调用一个win32本机c ++ dll。最初这是写在VS2010,并正在工作 - 我能够编组System :: String到std :: string,传递给本地dll,并cout的值。然后我将C#和c ++ / cli项目转换为VS2012以启用智能感知。这需要在VS2010中重新启用4.0 .NET框架的服务包安装。我在2010年重建Win32 dll,VS2012中的C#应用​​程序和c ++ / cli dll,现在我收到一个错误调用dll:



调试失败!



计划:
...
文件:f:\dd\vctools\crt_bld\self_x86\crt\ src\dbgheap.c
Line:1424



表达式:_pFirstBlock == pHead

  public ref class ManagedWrapper 
{
CSampleWin32Library * m_pUnmanagedWrapper;


public:
ManagedWrapper(){m_pUnmanagedWrapper = new CSampleWin32Library();}
〜ManagedWrapper(){delete m_pUnmanagedWrapper;}

//测试调用以证明集成
void Test(int x,System :: String ^ testString){
//封送处理示例:http://msdn.microsoft.com/en-us/library /bb384865.aspx
std :: string tmpStdString = marshal_as< std :: string>(testString);
m_pGambitUnmanagedWrapper-> Test(x,tmpStdString); // ERROR OCCURS HERE
};
};希望这是一样容易失去一些设置,或现在需要在VS2012。我没有改变任何代码否则,就我所知。

解决方案

这个错误大多是因为一个块



您应该看一看 Windows Via C / C ++ - 第四部分动态链接库。



应用程序将在运行时调用CRT。



有两种方式调用CRT - 链接到DLL C / C ++运行时库或链接到静态C / C ++运行时库,并且有不同版本的CRT。



使用不同的内存管理。



所以,你应该小心,当你释放内存和你的代码链接到DLL。


I have a C# form calling to a c++/cli interface DLL, which calls off to a win32 native c++ dll. Originally this was written in VS2010, and was working - I was able to marshal a System::String to a std::string, pass it to the native dll, and cout the value. I then converted the C# and c++/cli projects to VS2012 to enable intellisense. This required a service pack install to reenable the 4.0 .NET framework in VS2010. I rebuilt the Win32 dll in 2010, the C# app and c++/cli dll in VS2012, and now I receive an error on the call to the dll:

Debug Assertion Failed!

Program: ... File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c Line: 1424

Expression: _pFirstBlock == pHead

public ref class ManagedWrapper
{
    CSampleWin32Library* m_pUnmanagedWrapper;


public:
    ManagedWrapper() {m_pUnmanagedWrapper = new CSampleWin32Library();}
    ~ManagedWrapper() {delete m_pUnmanagedWrapper;}

    //Test call to prove integration
    void Test(int x, System::String^ testString) {
        //marshaling example: http://msdn.microsoft.com/en-us/library/bb384865.aspx         
        std::string tmpStdString = marshal_as<std::string>(testString);
        m_pGambitUnmanagedWrapper->Test(x, tmpStdString); //ERROR OCCURS HERE
    };      
};

Hopefully this is as easy as some setting that was lost, or is now required in VS2012. I didn't change any code otherwise, as far as I know.

解决方案

This error mostly is because of that a block of memory which you malloc in Heap A is freed in Heap B.

You should have a look at Windows Via C/C++--Part IV Dynamic-Link Libraries.

The application will call CRT when it's running.

There two ways to invoke CRT--link to the DLL C/C++ run-time library or link to the static C/C++ run-time library and there different versions of CRT.

All of them use different Memory Management.

So, you should be careful when you free the memory and your code linked to DLL.

这篇关于调试断言失败错误时,将std :: string从c ++ / cli dll传递给本机win32 dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆