Unmanaged C++ DLL with STL causes crash

  • Thread starter Thread starter Kurt Smith
  • Start date Start date
K

Kurt Smith

We are in the process of creating a managed C++ library to wrap some
existing unmanaged code. The unmanaged code makes use of the STL. We
are seeing NULL pointer exceptions whenever we construct our unmanaged
object. If we remove the dependency on STL in the unmanaged code the
crash goes away. Also, we can always instantiate the unmanged object
the first time but it fails on subsequent attempts. The managed code
is pretty simple. has anyone else seen this behavior?


// SimpleManaged.h

#pragma once

#include "./import/simpleunmanaged.h"
#pragma comment(lib, "./import/simpleunmanaged.lib")


public __gc class CSimpleManaged
{
private:
CSimpleUnmanaged __nogc* m_pSimpleUnmanaged;

public:
CSimpleManaged(void)
: m_pSimpleUnmanaged(new CSimpleUnmanaged())
{
}

~CSimpleManaged(void)
{
//System::Console::WriteLine(S"~CSimpleManaged");
if(NULL != m_pSimpleUnmanaged)
{
delete m_pSimpleUnmanaged;
m_pSimpleUnmanaged = NULL;
}
}

int Sum(int p_iData)
{
int rc = m_pSimpleUnmanaged->Sum(p_iData);
return(rc);
}

System::String* Concat(System::String* p_strData)
{
System::IntPtr ipStrData =
System::Runtime::InteropServices::Marshal::StringToHGlobalUni(p_strData);
wchar_t* pszData = static_cast<wchar_t*>(ipStrData.ToPointer());

wchar_t* pszResult = m_pSimpleUnmanaged->Concat(pszData);
System::String* strResult = new System::String(pszResult);

System::Runtime::InteropServices::Marshal::FreeHGlobal(ipStrData);

return(strResult);
}
};
 
Well, mixed mode DLLs are linked without an entry point so _DLLmainCRTStartup is not being called and CRT is not being initialized so static variables are not set, it is possible that the STL classes used by you use some static variables, if those variables are not initialized you would probably get errors during runtime, a detailed explanation of how to resolve this problem could be fond at the following URL: http://msdn.microsoft.com/library/d...tsfrompureintermediatelanguagetomixedmode.asp
 
Back
Top