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);
}
};
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);
}
};