C# to CLI call fails somewhere...

G

Guest

I have the below C# definitions and function call to an unsafe C++/CLI
function in an external dll. When debugging the calls step by step everything
seems to work, but if I just run everything through without stopping I get
the following error:

"
Debug Assertion Failed!

Program: .....myprogram..
File: dbgheap.c
Line: 1279

Expression: _CrtIsValidHeapPointer(pUserData)

....
"

If I press ignore three times the whole application crashes. Here is the code:

[DllImport(g_wrapper_dll_path)]
unsafe static extern int GetEncoders(
void* dvpsdk,
ref int[] devices_ids);



IntPtr m_DVPSDK_ptr;
void* l_ptr = m_DVPSDK_ptr.ToPointer();
int l_result = GetEncoders(
l_ptr,
ref m_devs_ids);



The C++/CLI functions:



extern "C" int GetEncoders(
System::IntPtr dvpsdk,
array<int>^ devices_ids)
{
return AdvantechImpl::GetEncoders(
(DVP1412DLL*) dvpsdk.ToPointer(),
devices_ids);
}



const int GetEncoders(
DVP1412DLL* dvpsdk,
array<int>^ devices_ids)
{
int l_nr_of_devices =
dvpsdk->DVP1412_GetNoOfDevices();

if (l_nr_of_devices == 0)
{
return -3;
}

int* l_tmp = new int[devices_ids->Length];

int l_res = dvpsdk->DVP1412_InitSDK(
l_nr_of_devices,
l_tmp);

if (l_res == SUCCEEDED)
{
for (int l_i = 0; l_i < devices_ids->Length; ++l_i)
{
devices_ids[l_i] = l_tmp[l_i];
}

l_res = dvpsdk->DVP1412_CloseSDK();
return l_nr_of_devices;
}
else
{
return l_res;
}
}
 
G

Guest

I should add that m_devs_ids has the following definition:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = g_max_devices)]
int[] m_devs_ids = new int[g_max_devices];
 
B

Ben Voigt [C++ MVP]

Joachim said:
I have the below C# definitions and function call to an unsafe C++/CLI
function in an external dll. When debugging the calls step by step
everything
seems to work, but if I just run everything through without stopping I get
the following error:

"
Debug Assertion Failed!

Program: .....myprogram..
File: dbgheap.c
Line: 1279

Expression: _CrtIsValidHeapPointer(pUserData)

...
"

If I press ignore three times the whole application crashes. Here is the
code:

[DllImport(g_wrapper_dll_path)]
unsafe static extern int GetEncoders(
void* dvpsdk,
ref int[] devices_ids);



IntPtr m_DVPSDK_ptr;
void* l_ptr = m_DVPSDK_ptr.ToPointer();
int l_result = GetEncoders(
l_ptr,
ref m_devs_ids);



The C++/CLI functions:



extern "C" int GetEncoders(
System::IntPtr dvpsdk,
array<int>^ devices_ids)
{
return AdvantechImpl::GetEncoders(
(DVP1412DLL*) dvpsdk.ToPointer(),
devices_ids);
}



const int GetEncoders(
DVP1412DLL* dvpsdk,
array<int>^ devices_ids)
{
int l_nr_of_devices =
dvpsdk->DVP1412_GetNoOfDevices();

if (l_nr_of_devices == 0)
{
return -3;
}

int* l_tmp = new int[devices_ids->Length];

int l_res = dvpsdk->DVP1412_InitSDK(
l_nr_of_devices,
l_tmp);

When l_nr_of_devices > devices_ids->Length, this function overflows the
buffer and corrupts the heap.

Also, where is l_tmp freed (you must call delete[])?

if (l_res == SUCCEEDED)
{
for (int l_i = 0; l_i < devices_ids->Length; ++l_i)
{
devices_ids[l_i] = l_tmp[l_i];
}

l_res = dvpsdk->DVP1412_CloseSDK();
return l_nr_of_devices;
}
else
{
return l_res;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top