12-07-2017 10:26 PM
Hi everyone,
We are trying to import C++ dll to Labview. As labview can't handle complex data types of C++ we have written a wrapper so that labview supports it. Now we are facing a problem while creating library .lvllb for this dll.
1. We have few functions which would return an array of values out of dll as output.
2. When we create a library, labview is not able to detect it as an array, so we made manual change to Array as the data type. In the configure window of Call library node-> parameter properties.
We are using Labview 2013 version. for importing the dlls
Because of this we are seeing that labview crashes randomly at different places of execution.
12-07-2017 11:46 PM - edited 12-07-2017 11:55 PM
I think the problem you're having here is related to your "Minimum Size" value.
The comment suggests that you should have a pointer to an array containing two doubles, eg
double a[2]; a[0] = 0.241; a[1] = 251.3211; long result = calcDarkSignal_DT(&a[0], ...);
However, your minimum size is set to 1, so LabVIEW will allocate either a) the minimum size (1) or b) the size passed in, if larger.
Since you don't wire an input array, your output will probably be outside the bounds of the memory. AFAIR, this is Undefined Behaviour.
Try increasing the minimum size to 2, if that is the returned size, or alternatively wire an input array with at least 2 elements.
Edit: I checked quickly and the C++ standard (or at least, the draft I found via Google) specifies as follows (regarding pointer arithmetic):
Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
Here we note that it's not undefined behaviour to have arithmetic involving the last+1 element (although I don't believe it is dereferenceable). As I understand it, that means probably your code won't tell you there's an error when you have an off-by-one error, but when you try and get the value, it will produce rubbish/memory corruption as you write in someone else's memory, etc, etc...
12-08-2017 12:03 AM
As a follow-up, the external code guide has the following to say on using arrays as output data from external code...
To return an array of data, you should allocate an array of sufficient size in LabVIEW, pass the array to your function, and have this array act as the buffer. If the data takes less space, you can return the correct size as a separate parameter and then, on the calling diagram, use array subset to extract the valid data.
See http://www.ni.com/pdf/manuals/370109b.pdf for more.