Adding and Releasing References
- Updated2026-07-27
- 1 minute(s) read
Maintaining a reference to an object guarantees that the object exists for at least at long as you hold the reference. The object frees its resources only when you release the final reference to the object.
Whenever you create an ActiveX object directly or receive an ActiveX object as a return value from a method or property, you have a reference to the object. You must release this reference when you no longer require the object.
Normally, when you receive an object as a parameter to a function or LabVIEW VI, you do not need to add or release a reference to the object because the caller of the function already holds a reference to the object. If, however, the function or VI stores the object for later use after the function or VI returns, you must add a reference to the object in the function or VI.
Refer to the following examples for more information about adding and releasing references in specific development environments:
- LabVIEW
- LabWindows/CVI
- Microsoft Visual Basic .NET
- C#
- Microsoft Visual C++/#import
Related Information
- Releasing ActiveX References in LabVIEW
- Adding and Releasing References in LabWindows/CVI
LabWindows/CVI automatically maintains an object reference for each handle you obtain for an object. When you assign the handle to another variable, LabWindows/CVI does not add a reference to the object. Use the CA_DuplicateObjHandle function in the LabWindows/CVI ActiveX Automation Library to obtain a new handle to an existing object, which adds a reference to the object. LabWindows/CVI automatically releases the object reference for each handle you obtain when you call the CA_DiscardObjHandle function from the LabWindows/CVI ActiveX Automation Library.
- Adding and Releasing References in Microsoft Visual Basic .NET
Microsoft Visual Basic .NET automatically adds object references when you assign the object to another variable. Visual Basic .NET also automatically releases object references when a variable or list that contains a reference to an object goes out of scope. Local variable scope ends when the function returns.
- Adding and Releasing References in C#
C# automatically adds object references when you assign the object to another variable. C# also automatically releases object references when a variable or list that contains a reference to an object goes out of scope. Local variable scope ends when the function returns.
- Adding and Releasing References in Microsoft Visual C++/#import
The pointer classes the #import directive generates automatically add object references when you construct a pointer. The destructor for the pointer class releases the object reference.