LabWindows/CVI

Managing Resources

The .NET framework provides different resource management tools, including garbage collection and the Dispose method, to help you allocate and free the resources that .NET applications use. The Resource Management function class in the LabWindows/CVI .NET Library contains functions that you can use to manage resources that are not automatically handled by the .NET framework resource management tools.

Allocating and Freeing Memory

There are certain instances in which you must programmatically allocate and free memory for use with generated wrapper functions and LabWindows/CVI .NET Library functions. You must call CDotNetAllocateMemory to allocate memory for any reference parameter input string or array. Likewise, you must call CDotNetFreeMemory to free the memory for any output parameter or reference parameter output strings or arrays. The function panel help for the generated wrapper functions and the .NET Library functions contains information about allocating and freeing memory for these parameters. If the output parameter or reference parameter output array contains strings, you must use CDotNetFreeMemory to free each string in the array and then use the same function to free the array itself. If the output parameter or reference parameter output array contains objects, you must use CDotNetDiscardHandle to discard each handle in the array and then use CDotNetFreeMemory to free the array itself.

Discarding Handles

When a .NET object is no longer being used, the garbage collection service frees resources allocated for that object. Constructors and other methods in the generated wrappers as well as functions in the .NET Library, such as CDotNetCreateInstance and CDotNetObjectGetType return or output object handles. When you finish using an object, you must call CDotNetDiscardHandle to discard the object handle. If you do not discard the object handle, the .NET garbage collection service does not recognize that the object is no longer being used and does not free the resources assigned to that object. You must discard object handles to ensure proper garbage collection.

In most cases, the generated wrapper manages loading and unloading assemblies. However, if you must directly call a .NET Library function that requires an assembly handle, you must call CDotNetLoadAssembly to explicitly load the assembly and access its handle. When you finish using that assembly, you must call CDotNetDiscardAssemblyHandle to discard the assembly handle.

Using Disposable Types

Depending on the application and the resources an object within an application uses, garbage collection might not be sufficient to effectively free unused memory. .NET types that use valuable or critical resources can include a Dispose method, which you can call to free object resources immediately without waiting for garbage collection to do so. After you call the generated wrapper function for the Dispose method, you must call CDotNetDiscardHandle to discard the handle for the object whose resources you freed.

The method that frees disposable type resources is typically named Dispose; however, some .NET assemblies might not use this naming convention. Furthermore, it is possible to modify a generated wrapper function name so that it does not match the method name exactly. National Instruments recommends you refer to the generated wrapper function panel documentation to determine what generated functions correspond to Dispose methods.