472,805 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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
2 2263
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy | last post by:
Hi, I've got some code which uses various "On Error" statements to try to trap some errors in VB6. When I try it on one machine (WinXP SP1) it works fine, but on another machine (WinXP SP2) it...
4
by: Brano Zarnovican | last post by:
Hi ! If I define 'f' like this def f(a): print a then, the call with keywords f(1, optional=2)
3
by: jasonkester | last post by:
Just a heads up for anybody that comes across this in the future. Noticed a strange behavior in RegExp.test() today. Check out the following code. It will alternately display "chokes" and null,...
2
by: Yogi21 | last post by:
Hi I have a sorted array containing strings. I am iterating through the array and clearing the contents one by one using "array.BinarySearch" to find each element. So far so good. But the moment I...
1
by: drewmania001 | last post by:
i've read various info on the web including the following http://php.mirrors.ilisys.com.au/manual/en/ref.mysqli.php mySQL Version 4.1.16 PHP Version 5.1.2 with Zend Engine v2.1.0 OS Windows...
2
by: Toolsmith | last post by:
The following code: C# code: Directory.CreateDirectory("D:\\somewhere\\somewhereelse\\here\\") works, but the following does flags the error: An unhandled exception of type...
54
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
2
by: rdilipk | last post by:
I am posting at the end of this post some code that P/Invoke's SetSystemTime to set the local system time. This call fails -- i.e the time is not set and the API returns false. However calling...
2
by: Philippe Poulard | last post by:
Hi folks ! I have some code like this that passes an anonymous function: foo.bar(p1, function(p2) { alert(p2); }); in foo.bar, i create a button with a listener; when it is executed, the...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.