LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VC++ code crashes LabView

Hello,

I am new to Labview and am having some trouble using CIN to
interact with Visual C++ .Net.

I am trying to read in some numbers from my Visual C++ .Net program
into Labview 7.1. The C++ code works on images and retrieves some
object locations. I need to transfer these numbers to Labview, and for
this I use the CIN.

I set up the basics to work with CIN and built the .lsb file
successfully. This is what I did initially. When an input switch is on
(boolean value in Labview front panel), I sent out one set of values
as outputs and another set of values when the switch is off. I
resolved the usual linker and other issues and this program works
fine.

Next, I included my actual code into the project and called a
function to do the image calculations. After building the .lsb file
and updating the CIN resource in Labview, running it caused Labview to
exit. I tried playing around with the C++ code. This is my CIN
function in VC++

MgErr CINRun(LVBoolean *start, int32 *torso, int32 *left, int32
*right)
{
}

When I assign specific integer values to *torso, *left and *right
they work fine (as in the initial test case above). But when I assign
these values from an array that I compute, Labview exits without
warning.
That is - *torso = 10; *left = 10; *right = 20; //works fine
but - *torso = array[val1]; *left = array[val2]; *right =
array[val3]; //causes Labview to close

I read in an earlier post that Labview has some incompatibility
with a particular version of .Net. Could this be an issue? Or is it
human error?

TIA

0 Kudos
Message 1 of 9
(2,995 Views)
On Feb 8, 3:45 pm, "Bala L" <balasubramania...@gmail.com> wrote:
> Hello,
>
> I am new to Labview and am having some trouble using CIN to
> interact with Visual C++ .Net.
>
> I am trying to read in some numbers from my Visual C++ .Net program
> into Labview 7.1. The C++ code works on images and retrieves some
> object locations. I need to transfer these numbers to Labview, and for
> this I use the CIN.
>
> I set up the basics to work with CIN and built the .lsb file
> successfully. This is what I did initially. When an input switch is on
> (boolean value in Labview front panel), I sent out one set of values
> as outputs and another set of values when the switch is off. I
> resolved the usual linker and other issues and this program works
> fine.
>
> Next, I included my actual code into the project and called a
> function to do the image calculations. After building the .lsb file
> and updating the CIN resource in Labview, running it caused Labview to
> exit. I tried playing around with the C++ code. This is my CIN
> function in VC++
>
> MgErr CINRun(LVBoolean *start, int32 *torso, int32 *left, int32
> *right)
> {
>
> }
>
> When I assign specific integer values to *torso, *left and *right
> they work fine (as in the initial test case above). But when I assign
> these values from an array that I compute, Labview exits without
> warning.
> That is - *torso = 10; *left = 10; *right = 20; //works fine
> but - *torso = array[val1]; *left = array[val2]; *right =
> array[val3]; //causes Labview to close
>
> I read in an earlier post that Labview has some incompatibility
> with a particular version of .Net. Could this be an issue? Or is it
> human error?
>
> TIA

It looks like the problem is because of the file operations I perform
to read/write the image. When I remove the file ops in the file-write
function, labVIEW no longer crashes. Also, it looks like file-read is
not loading the image from the file to the memory.

Do I have to change settings somewhere to handle file ops while using
CIN? I did add the CINProperties() function to the CIN code to make
labVIEW recognize my code as being thread-safe. But that hasn't
changed anything.

0 Kudos
Message 2 of 9
(2,987 Views)


@Bala L wrote:

It looks like the problem is because of the file operations I perform
to read/write the image. When I remove the file ops in the file-write
function, labVIEW no longer crashes. Also, it looks like file-read is
not loading the image from the file to the memory.

Do I have to change settings somewhere to handle file ops while using
CIN? I did add the CINProperties() function to the CIN code to make
labVIEW recognize my code as being thread-safe. But that hasn't
changed anything.


No special settings should be necessary. Are you sure you allocate the array where you
write in your values inside your C code either explicitedly on the heap or implicitely on the
stack with a big enough size (int array[100] for instance)?
 
It is most likely something like this that goes wrong rather than functions you couldn't run
from a CIN (I'm not aware of any since LabVIEW doesn't support Windows 3.1 anymore).
Also enabling reentrancy is not making things safer but actually more delicate. If your code
isn't really reentrant safe you can produce anything from a crash to a deadlock or computing
bad data but I would guess this not to be the problem just yet.
 
Rolf Kalbermatter 

 
Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 9
(2,979 Views)
On Feb 9, 3:10 am, rolfk <x...@no.email> wrote:
> Bala L wrote:It looks like the problem is because of the file operations I performto read/write the image. When I remove the file ops in the file-writefunction, labVIEW no longer crashes. Also, it looks like file-read isnot loading the image from the file to the memory.Do I have to change settings somewhere to handle file ops while usingCIN? I did add the CINProperties() function to the CIN code to makelabVIEW recognize my code as being thread-safe. But that hasn'tchanged anything.
>
> No special settings should be necessary. Are you sure you allocate the array where you
> write in your values inside your C code either explicitedly on the heap or implicitely on the
> stack with a big enough size (int array[100] for instance)?
> &nbsp;
> It is most likely something like this that goes wrong rather than functions you couldn't run
> from a CIN (I'm not aware of any since LabVIEW doesn't support Windows 3.1 anymore).
> Also enabling reentrancy is not making things safer but actually more delicate. If your code
> isn't really reentrant safe you can produce anything from a crash to a deadlock or computing
> bad data but I would guess this not to be the problem just yet.
> &nbsp;
> Rolf Kalbermatter&nbsp;
> &nbsp;

Thanks for the reply, Rolf.

I allocate memory for the arrays dynamically in the C++ code to hold
the image. After my calculations, I assign the desired values to the
function's return variables before releasing the array memory. I am
pretty certain that the C++ code works correctly since I didn't have
any memory leaks or weird behaviour when I ran my C++ code in the .NET
IDE.

A question about reentrancy - does reentrance mean that the CIN
function is called repeatedly by more than one functions? Or is my
code reentrant even if other parts of the code are multi-threaded?

0 Kudos
Message 4 of 9
(2,963 Views)
On Feb 9, 9:18 am, "Bala L" <balasubramania...@gmail.com> wrote:
> On Feb 9, 3:10 am, rolfk <x...@no.email> wrote:
>
> > Bala L wrote:It looks like the problem is because of the file operations I performto read/write the image. When I remove the file ops in the file-writefunction, labVIEW no longer crashes. Also, it looks like file-read isnot loading the image from the file to the memory.Do I have to change settings somewhere to handle file ops while usingCIN? I did add the CINProperties() function to the CIN code to makelabVIEW recognize my code as being thread-safe. But that hasn'tchanged anything.
>
> > No special settings should be necessary. Are you sure you allocate the array where you
> > write in your values inside your C code either explicitedly on the heap or implicitely on the
> > stack with a big enough size (int array[100] for instance)?
> > &nbsp;
> > It is most likely something like this that goes wrong rather than functions you couldn't run
> > from a CIN (I'm not aware of any since LabVIEW doesn't support Windows 3.1 anymore).
> > Also enabling reentrancy is not making things safer but actually more delicate. If your code
> > isn't really reentrant safe you can produce anything from a crash to a deadlock or computing
> > bad data but I would guess this not to be the problem just yet.
> > &nbsp;
> > Rolf Kalbermatter&nbsp;
> > &nbsp;
>
> Thanks for the reply, Rolf.
>
> I allocate memory for the arrays dynamically in the C++ code to hold
> the image. After my calculations, I assign the desired values to the
> function's return variables before releasing the array memory. I am
> pretty certain that the C++ code works correctly since I didn't have
> any memory leaks or weird behaviour when I ran my C++ code in the .NET
> IDE.
>
> A question about reentrancy - does reentrance mean that the CIN
> function is called repeatedly by more than one functions? Or is my
> code reentrant even if other parts of the code are multi-threaded?

I have tried using C-level functions instead of C++ functions for file
ops. Also tried moving the vi file to the same directory as the .dll
and .lsb files (previously, it was in a parent directory). Neither of
these helped to solve the issue. Labview still shuts down when CIN
function calls the file writing function.

Any ideas on this one?

0 Kudos
Message 5 of 9
(2,951 Views)


@Bala L wrote:
> I allocate memory for the arrays dynamically in the C++ code to hold
> the image. After my calculations, I assign the desired values to the
> function's return variables before releasing the array memory. I am
> pretty certain that the C++ code works correctly since I didn't have
> any memory leaks or weird behaviour when I ran my C++ code in the .NET
> IDE.
>
> A question about reentrancy - does reentrance mean that the CIN
> function is called repeatedly by more than one functions? Or is my
> code reentrant even if other parts of the code are multi-threaded?


If you designate your CIN to be reentrant LabVIEW will call it in whatever thread it is
executing the rest of the diagram. As long as you call that CIN only in one place or can
make sure with data dependency that it is not called in parallel somehow there is
nothing at hand.
But once you call the CIN at different places in LabVIEW that could in fact run in
(pseudo)parallel then you are in trouble when the CIN code or libraries it relies on
are not multithreading safe.
Use of unprotected global variables is for instance inherent multithreading unsafe.
The same applies for static local variables which also are in fact like global variables
in C but with a local scope.
If your code is reentrant (aka multithreading safe) is something only you as programmer
can know. If you tell LabVIEW it is, it will not take any precautions to protect your code
from being called from different places at the same time.

I stil think you do somewhere something wrong. It could be something simple as a
forgotten * dereferencing and most probably when you do source level deugging you
will see it at some point and hit yourself for not having noticed earlier.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 9
(2,944 Views)
"It could be something simple as aforgotten * dereferencing and most
probably when you do source level deugging youwill see it at some
point and hit yourself for not having noticed earlier."
--I figured out why the file wasn't being read properly - I wasn't
assigning enough memory for the file.

I am still having the issue of Labview closing down during a file-
write operation. This is the structure of the C++ code:

// file1.cpp
MgErr CINRun(LVBoolean *start, int32 *t_i, int32 *t_j)
{
/* Insert code here */
func1(start, t_i, t_j);
return noErr;
}

// file2.cpp
void func1(LVBoolean *start, int32 *t_i, int32 *t_j)
{
if(*start == true)
{
CImg img(ROW, COL, FRAMES); // Image object
int torso, cranial;
img.fileRead(INPUT_FILENAME); // Works fine
img.fileWrite(OUTPUT_FILENAME); // Works fine
img.detectShape(torso, cranial);
t_i = torso;
t_j = cranial;
}
else
{
t_i = 0;
t_j = 0;
}
}

The call to detectShape() does all the calculations correctly. At
the end of this function, when I write out an image file with the
detected results, it causes Labview to close down. I tried debugging
the code in the .NET IDE through Labview using the online reference
("using external code in labview" pdf document). The file isn't being
opened correctly, and it is causing the program to exit. The first
fileWrite() is working fine. I'm trying to figure out why this is
happening.

0 Kudos
Message 7 of 9
(2,913 Views)


@Bala L wrote:
"It could be something simple as aforgotten * dereferencing and most
probably when you do source level deugging youwill see it at some
point and hit yourself for not having noticed earlier."
--I figured out why the file wasn't being read properly - I wasn't
assigning enough memory for the file.

I am still having the issue of Labview closing down during a file-
write operation. This is the structure of the C++ code:

// file1.cpp
MgErr CINRun(LVBoolean *start, int32 *t_i, int32 *t_j)
{
/* Insert code here */
func1(start, t_i, t_j);
return noErr;
}

// file2.cpp
void func1(LVBoolean *start, int32 *t_i, int32 *t_j)
{
if(*start == true)
{
CImg img(ROW, COL, FRAMES); // Image object
int torso, cranial;
img.fileRead(INPUT_FILENAME); // Works fine
img.fileWrite(OUTPUT_FILENAME); // Works fine
img.detectShape(torso, cranial);
t_i = torso;
t_j = cranial;
}
else
{
t_i = 0;
t_j = 0;
}
}

The call to detectShape() does all the calculations correctly. At
the end of this function, when I write out an image file with the
detected results, it causes Labview to close down. I tried debugging
the code in the .NET IDE through Labview using the online reference
("using external code in labview" pdf document). The file isn't being
opened correctly, and it is causing the program to exit. The first
fileWrite() is working fine. I'm trying to figure out why this is
happening.



As I thought! t_i and T_j are references to integers, but you just assign them with 0 or the cranial and torso integers. Maybe this code would work in a pure C++ program, I'm not sure, but in C it is important to assign the variables correctly.

So change

t_i = torso;
t_j = cranial;
}
else
{
t_i = 0;
t_j = 0;
}
}

to

*t_i = torso;
*t_j = cranial;
}
else
{
*t_i = 0;
*t_j = 0;
}
}

instead!

And you probably want to change the torso and cranial parameters to DetectShape to read &torso and &cranial too.

Quite a typical and simple bug most C progammers do in the beginning.

Rolf Kalbermatter

Message Edited by rolfk on 02-13-2007 04:38 PM

Rolf Kalbermatter
My Blog
0 Kudos
Message 8 of 9
(2,910 Views)
> As I thought! t_i and T_j are references to integers, but you just assign them with 0 or the cranial and torso integers. Maybe this code would work in a pure C++ program, I'm not sure, but in C it is important to assign the variables correctly.So change
> t_i = torso;
> t_j = cranial;
> }
> else
> {
> t_i = 0;
> t_j = 0;
> }}to
>
> *t_i = torso;
> *t_j = cranial;
> }
> else
> {
> *t_i = 0;
> *t_j = 0;
> }
>
> }instead!

Oops, that was a typo. I assign it using
*t_i = torso;

and not
t_i = torso;

C++ won't allow a conversion from integer to a pointer unless it is
explicit 🙂 I messed it up when I was cleaning the code for this post.

> And you probably want to change the torso and cranial parameters to DetectShape to read &amp;torso and &amp;cranial too.

We don't need them in the function call, do we? The function
definition contains the ampersands, so the integers are being passed
as references.

Since the problem is the fileWrite() function called from within
detectShape(), I have removed this file writing bit for now. I am not
sure if this is a part of a bigger problem...

0 Kudos
Message 9 of 9
(2,904 Views)