Closing References to VIs and VI Objects Using VI Scripting

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 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:

Action Side Effect
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:

Close Reference
 Add  Find

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.

 Open Example

Expand the procedure for more information about this specific example.

  1. 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
  2. Determine the last time that LabVIEW uses each reference.

    Consider the following issues when tracing the data flow of a reference:

    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
  3. 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

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.

 Open example  Find related examples