LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using .NET close/dispose method?

Solved!
Go to solution

I have a question about .NET in LabVIEW. When a .NET instance is created with a 'Constructor Node' what is the correct way to close the reference?

  • just close the reference with 'Close Reference'
  • use a Close or Dispose method, if the .NET instance provides such methods, and then close the reference

I think the later should be the right way to do it, but I am not sure if LabVIEW does something behind the scenes, and just Close would be sufficient.

 

The attached VI is just an example. Probably calling the Performance Counter 'Close' method before the 'Close Reference' is how it should be done.

BTW: More info about the Performance Counter can be found here

 

example: performance_counter_cpu_load.png

 

.

 

 

 

 

 

I use a simple VI to get the CPU load of the system.

Message 1 of 2
(4,125 Views)
Solution
Accepted by chembo

Close and Dispose are just method calls on a class. Despite their common name they do not hold any special meaning in LabVIEW (and meaning in .NET through convention only via the IDisposable interface). Thus whether they need to be called or not for "clean-up" is down to the implementation of the particular class itself, which you can find on MSDN (https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter(v=vs.110).aspx). In this example, MSDN states that Close cleans-up resources and that Dispose will perform the same cleanup.

 

If the MSDN description leaves room for doubt then Microsoft also publishes the source code (in C#) for classes in the BCL. PerformanceCounter is obviously one of those classes. I looked at the source here: PerformanceCounter.cs - the link goes directly to the Dispose method. The Close method does clean-up references and the Dispose method will also perform the same cleanup (base class calls protected override with disposing=true).

 

The MSDN documentation also states that the Dispose method renders the object unusable, so the object should be immediately un-pinned by using the LabVIEW Class References node to avoid accidental use after the Dispose call.

 

Edit: Regarding your question about whether to use Close References node or not; always use it on any .NET object you are finished using, especially ones that you have obtained through calling methods on another .NET object (these are easy to forget). Behind the scenes LabVIEW "pins" the object in memory so that .NET CLR does not perform garbage collection and attempt to move it's location in memory / remove the object prematurely. In order for LabVIEW to un-pin the object you need to either explicitly request this (via the Close Reference node) or wait for the current VI hierarchy to go idle which will perform this automatically for you (but this requires your application to complete and stop; only good for long term references).

Message 2 of 2
(4,098 Views)