467,923 Members | 1,449 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

C# to CLI call fails somewhere...

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

Oct 5 '07 #1
  • viewed: 2124
Share:
2 Replies
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];
"Joachim" wrote:
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;
}
}
Oct 5 '07 #2

"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:19**********************************@microsof t.com...
>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;
}
}
Oct 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Brano Zarnovican | last post: by
3 posts views Thread by jasonkester | last post: by
2 posts views Thread by Yogi21 | last post: by
1 post views Thread by drewmania001 | last post: by
2 posts views Thread by Toolsmith | last post: by
2 posts views Thread by Philippe Poulard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.