LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing back .NET object from Callback VI to main VI

Hello all!

 

I'm coming from C++ and zero Labview and .NET experience.

 

My library in C++/CLR (.NET) generates an event that returns a System::Array of a struct (so, an array of .NET objects).

 

In Labview, i'm trying to attach a callback VI and then pass the array of structs back to the main VI using events:

 

Screenshot_20231116_153917.png

mosfetlover_420_2-1700146475535.png

 

The problem is that when I access the fields of the structs passed back into the main VI, i get strange and different errors, for example:

 

mosfetlover_420_0-1700145867364.png

 

However, if inside the callback VI I loop over the array and then copy all the structs to an array of native Labview objects and then pass back the native array to the main VI, everything works good.

 

This make me think that the problem is that when I pass the array of NET structs back to the main VI, for some reason the structs got garbage collected and I try to access a deleted struct.

 

So I'm asking for help about:

  1. The problem I'm facing is due to structs be garbage collected before being elaborated from the main VI, as I imagine?
  2. How can I prevent that?
  3. If that's not possible, how I can I pass my array of NET struct from callback to main VI?

As I said, looping and copy the structs fields to native LV objects inside the callback VI works but Labview is painfully slow doing this so I would like to avoid that.

 

Thanks!

0 Kudos
Message 1 of 5
(562 Views)

Can you post the code of the event structure where the user event data comes back in and gets processed?

 

Error 1 often means a reference is the default/null value, and in LabVIEW it's possible that you may have done something with the code that reverted your array to contain default values instead.   Someone new with LabVIEW might not notice this happening... one very common thing to happen is that the default output from an event structure is set to "Use default if unwired", so if your event return data passes through one of those it can null the references.

 

Posting the full VI instead of screenshots would be best.  If you do, please do a "save for previous version" before saving, as I am on LabVIEW 2021 and many other forum members are on earlier versions too, so saving it back to 2019 or 2018 maximizes the amount of people who can peek at it to check for issues.

0 Kudos
Message 2 of 5
(528 Views)

@mosfetlover_420 wrote:

 

So I'm asking for help about:

  1. The problem I'm facing is due to structs be garbage collected before being elaborated from the main VI, as I imagine?

LabVIEW requests the CLR to pin all .NET object references that your block diagram exposes - either by requesting the creation of a new object (constructor) or an object that is returned from a method or property. This is specifically to prevent the situation you are describing, as well as prevent the GC from moving those objects when defragmenting.

 

The consequence though is that, once you are done, you should use the Close Reference node on those .NET references - this lets the CLR know to un-pin them allowing the GC to do it's work. LabVIEW will also do this for you automatically when the top-level VI (your entry point) finishes.

 

As has been said, the problem is likely elsewhere, possibly in the setup of your event structure. Posting your code will help.

0 Kudos
Message 3 of 5
(514 Views)

 

However, if inside the callback VI I loop over the array and then copy all the structs to an array of native Labview objects and then pass back the native array to the main VI, everything works good.

 

This make me think that the problem is that when I pass the array of NET structs back to the main VI, for some reason the structs got garbage collected and I try to access a deleted struct.

 

 


Hi

I was in similar situation, used to send .Net ref from A.vi to Main.vi using notifiers and the reference was getting null when received at the Main vi.
The workaround we used was to send .NET ref as variant using .Net obj to variant function and converting back to .net obj at main VI. This solved the problem. Try it and see if it helps.

bharathp10_0-1700197566353.png

In our case it was not a callback vi and it was going idle after sending the .Net reference and this might cause the reference to become invalid.

Thanks

 

bp
0 Kudos
Message 4 of 5
(483 Views)

Thanks for all the suggestions!

 

I'll try them. In the meanwhile, this is my event handle loop:

 

Screenshot_20231117_110244.png

Th event handle loop just checks if the returned array has at least one element and then gets an uint64_t property and prints it on the panel.

 

I'll also attach my VI and two dependencies VI, but I can not share the DLL (which would require custom hardware to work, thus would be useless).

 

Thanks again!

Download All
0 Kudos
Message 5 of 5
(455 Views)