Closing References to VIs and VI Objects Using VI Scripting
- Updated2025-08-15
- 3 minute(s) read
Requires: VI Scripting
All VI Scripting tasks require you to open references to the VIs and objects that you want to create, edit, or inspect. You create or open a reference when you use VIs, functions, properties, and methods that return a reference. As long as a VI reference remains open, LabVIEW cannot remove the corresponding VI from memory. Therefore, when a large number of references are open, a large number of VIs and objects are in memory, causing LabVIEW to perform more slowly and possibly preventing LabVIEW from creating new VIs and objects if no more memory is available. To avoid these performance issues, close references to VIs or objects that you open within a scripting VI.
You do not have to close the following types of references since leaving them open does not impact performance:
- References that inherit from GObject
- This VI or This Application static references
Use the Close Reference function to close references that you create or open. Note that the following actions close the reference as part of their operation, so you do not need to call Close Reference.
| Action | Result |
|---|---|
| Using the Generic:Delete method | Closes the reference to the object that you are deleting |
| Using the GObject:Replace method | Closes the reference to the object that you are replacing |
| Closing a reference to a VI | May invalidate references to objects within the VI |
What to Use
This VI Scripting task requires the following objects:
What to Do
Before you start: Open the Closing References VI in the labview\examples\Application Control\VI Scripting\Managing References directory for an example that illustrates each step of the following procedure.
- Identify references to VIs and VI objects on the block diagram of the scripting VI.
Ignore the previously mentioned reference types that you do not need to close.
Example Details
In the example, the scripting VI opens a reference in the following places:
- Open VI Reference—Opens a reference to the target VI
- Traverse for GObjects—Opens a reference to each Function object in the target VI
- Function:Terminals[]—Opens references to all the terminals of each function
- Terminal:Connected Wire—Opens a reference to the wire that is connected to each terminal
- Wire:Terminals[]—Opens references to all the terminals of a wire
- Function:Replace—Opens a reference to the replacement function
- Function:Terminals[]—Opens references to all the terminals of the replacement function
- Determine the last time that LabVIEW uses each reference.
When tracing the data flow of a reference, remember that LabVIEW requires an open reference to a node when you use a reference to any terminal of that node.
Example Details
In the example, the open references are last used in the following places:
- Target VI reference—At the end of the VI. LabVIEW uses this reference throughout the VI because the other open references are to objects within the target VI.
- Function references—After the Function:Replace method
- Function terminal references—After the Terminal:Connected Wire method
- Wire reference—After the Wire:Delete method
- Wire terminal references—After the Terminal:Connect Wire method
- Replacement function reference—After the Function:Terminals[] method
- Terminal references for replacement function—After the Terminal:Connect Wire method
- Wire each open VI reference to a Close Reference function soon after LabVIEW last uses a reference to that VI or one of its object references. Use the data flow provided by the error cluster to control when LabVIEW executes each Close Reference function. Beware of the results when closing references.
Caveats and Recommendations
- Close object references once you are done using all object references within that VI —When you close a reference to a VI, all open references to objects within that VI might become invalid. You can safely close a VI reference only after you finish using both the VI reference and all references to objects within that VI.
- Close references as soon as possible —An excessive number of open references causes LabVIEW to run more slowly. For small applications, this effect is negligible, but large applications will exhibit poorer performance. When you close references early in the program, you prevent them from lingering in memory unnecessarily.
- Close references before the end of a VI when possible —LabVIEW automatically closes references in a VI that has finished executing, so you do not improve performance by closing references at the end of a VI.
Additional Examples
Refer to the Creating New VI From Scratch VI in the labview\examples\Application Control\VI Scripting\Creating VIs directory for another example of closing many references when using VI Scripting.