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 reference remains open, LabVIEW cannot remove the corresponding VI or object 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. Thus, to help avoid these performance issues, close all references to VIs or objects that you open within a scripting VI.
Use the Close References function to close any references that you create or open. However, you cannot close references that LabVIEW closes as a side effect of the following actions:
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.
Open Example
Expand the procedure for more information about this specific example.
Identify all references to VIs and VI objects on the block diagram of the scripting VI.
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.
Consider the following issues when tracing the data flow of a reference:
LabVIEW requires an open reference to a VI when you use a reference to any object within that VI.
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 reference to a Close Reference function soon after LabVIEW last uses the reference. Use the data flow provided by the error cluster to control when LabVIEW executes each Close Reference function. Beware of the side effects when closing references.
Example Details
In the example, the scripting VI closes most references immediately after their final use. However, the example contains two unique cases where this is not possible:
The Function:Replace method closes the reference to the function that it replaces. Therefore, the scripting VI does not need to close that reference after the Function:Replace method finishes executing.
The Wire:Delete method closes the reference to the wire that it deletes. Therefore, the scripting VI cannot close that reference after the Wire:Delete method finishes executing.
Caveats and Recommendations
Close object references before closing references to their containing VI—When you close a reference to a VI, all open references to objects within that VI might become invalid. Thus, 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.