2
votes

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.

1
Passing C++ objects across DLL boundaries is troublesome. You are being reminded about it, your C++/CLI DLL doesn't use the same version of the CRT as the C++ DLL. You'll have to rebuild the C++ DLL so it uses the VS2012 version of the CRT, make sure that /MD is in effect. Further improve Test() by passing the std::string by reference instead of by value. - Hans Passant
Thanks Hans. Would you mind submitting this as the answer, so I can accept? - reuscam
It was just a guess, there's little in the question to suggest this is the real cause of the problem. It is best for you to post your own answer and describe exactly what you found. - Hans Passant

1 Answers

1
votes

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.