01-05-2006 08:59 PM - edited 01-05-2006 08:59 PM
Message Edited by Dynamik on 01-05-2006 09:00 PM
01-05-2006 11:01 PM
01-06-2006 12:48 PM
Don't forget that references for external applications, such as ActiveX and .NET references must be closed explicitly. Other than that, Leonid's response is very appropriate. Users should only see memory or performance issues in large applications or applications with references opened in loops with many expected iterations if they don't explicitly close their references.
You might also be careful about your memory management when targeting a real-time device, since they typically have much less memory than desktop PCs, and a memory overflow will cause the entire target to reboot.
01-06-2006 01:34 PM
01-06-2006 02:02 PM
FWIW, I have noticed that, at least in LabVIEW v7.1.1, when I get a reference from a property node of some object, the help window for that reference property sometimes says that the the reference must be closed when you are finished using it.
I don't know if that means that the ones that don't say anything don't require closing...
01-06-2006 03:01 PM - edited 01-06-2006 03:01 PM
Here's my take on closing references. My #1 rule regarding references is to always close them, no matter where they came from. I have two (hopefully obvious) exceptions to this rule...I don't close static references (like Control Reference, This VI, This App, etc.), and I don't close the reference output coming from an implicit App or VI property/invoke node (you know, the kind where you don't wire the reference input, and thus they refer to the current App or VI). Everything else I close.
That being said...
There are some references (as of LabVIEW 7.0 and later, I think) that are ok to not close because LabVIEW will automatically close them for you once your VI goes idle (i.e. it stops running, and any VIs that call it stop running). Primarily, these references come from property nodes, i.e. the "Controls[]" property of the Panel class. If you get an array of controls on the panel via this method, you will be ok if you don't close them all, because they'll be automatically closed once the VI goes idle. Even if you get this array in a loop, and you don't close the references, you should be ok, because the property node will return the same references each iteration of the loop, i.e. it won't return a new reference to the same object every iteration. Again, it didn't always behave this way, but it has since (I think) LabVIEW 7.0...maybe even earlier.
However, for pretty much all other kinds of references, you need to close them to avoid leaks. This includes any VI reference, like those you obtain from an Open VI Reference, or even those VI references obtained from property nodes (like the example given in the previous post). Also, *any* reference you get for ActiveX, .NET, or some other non-VI Server interface, needs to be closed.
So, since there are some references that *must* be closed to avoid leaks, and there are some references where it's ok to not close them, it's best to be safe and always close everything (see my #1 rule above).
-D
Message Edited by Darren on 01-06-2006 03:03 PM
01-06-2006 09:48 PM
Thanks for all the pointers!
@Dynamik wrote:
... there's been a lot of talk about why references must always be closed - and I'm sure some should be - but which?
01-06-2006 10:58 PM
Hi again,
Yes, the Close Reference function is a no-op on those references I mentioned that the VI will close for you when it goes idle. You can see this for yourself with a simple VI where you get the Control reference array for the panel, index off a control, read a property of the control, close the control reference, use the *same* reference wire you just closed, and read another property of the control. The property will read just fine, and you will not get an error out of the property node (like you normally would if you had an invalid reference)...this is because the Close Reference function did not do anything because this was one of those special references that the VI will "clean up" when it goes idle. I have attached a screenshot of a VI that demonstrates this.
Hope this clears things up a little bit. Remember my rule though, always close the references, even if they're the kind that don't really get closed. It's just good coding practice.
-D
01-07-2006 01:17 AM - edited 01-07-2006 01:17 AM
Hi Darren,
I truly appreciate your posts - and read them carefully. That's a powerful observation about closing Controls[] references having no effect! Now I know never to use a for-loop to be sure all the Controls[] references are closed.
But in the context of this thread, the following statements might be slightly misleading
I don't close static references (like Control Reference, This VI, This App, etc.)
... the Close Reference function did not do anything [to a Controls[] reference] because this was one of those special references that the VI will "clean up" when it goes idle.
I agree, no harm to close it - when one doesn't know what needs to be closed. But, maybe, better to omit extraneous, usless operations?
So the question remains, how to know what's useless, and what's not. Warren's observation is particularly relevent here - truth may be in "The Manual".
.
Message Edited by Dynamik on 01-07-2006 01:19 AM
01-09-2006 12:52 AM