LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best Practice for closing References (.NET)

Solved!
Go to solution

See picture attached: 

 

I have a loop that runs likely indefinite, and i have some .NET invoke nodes inside. 

How do i wire this up in the best way ? I want to avoid memory/reference leaks + unneccesary overhead as much as possible.

 

Is one of the two options on the pic an option or is there any better solution?

0 Kudos
Message 1 of 4
(2,340 Views)

Create and destroy the reference outside of the loop. Use a shift register to keep the reference in the loop.

So technically, both options you show aren't the recommended way...

However, the top one with exchange of tunnels to shift register is the recommended way.

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 2 of 4
(2,325 Views)

why a shift register ? the reference keeps always the same, even without.

0 Kudos
Message 3 of 4
(2,264 Views)
Solution
Accepted by topic author Philipp_Elhaus

Technically you are most likely right. Defensive programming however says:

You shall not make assumptions!

It's currently the case that a LabVIEW node does not regenerate a refnum. But in the longer run there may be a chance, although fairly unlikely that LabVIEW may decide to to have to recreate a refnum for whatever reason.

 

More importantly though it is a not so uncommon case that you may end up at some time with code that explitly has to destroy an object and recreate it for whatever reason and if you or whoever will have to do that modification, forgets to change the tunnel to a shift register, the code will behave badly. By making it a shift register from the beginnning you avoid this potential pitfall in the future. This is defensive programming, by anticipating future modifications that may require additional functionality that does not cost you anything when implemented right from the beginning.

 

You are reusing the refnum yes and a shift register is very clear about what is actually happening. Everything else is assuming things, or as your second solution highly inefficient by recreating the object at every iteration.

Rolf Kalbermatter
My Blog
Message 4 of 4
(2,253 Views)