LabWindows/CVI

Releasing Resources when a DLL Unloads

When a program terminates, the operating system disposes resources that your DLL allocates. If your DLL remains loaded throughout program execution, the DLL does not need to dispose resources explicitly when the system unloads the DLL at program termination. However, if the program unloads your DLL during program execution, it is a good idea for your DLL to dispose of any resources it allocates. The DLL can release resources in the DllMain function in response to the PROCESS_DETACH message. Additionally, the DLL can release resources in a function that it registers with the ANSI C atexit function. The system calls the function you register when the DLL receives the PROCESS_DETACH message.

If your DLL calls into the LabWindows/CVI Runtime DLL, it can allocate resources such as user interface panels. If a program unloads your DLL during execution, you might want to dispose these resources by calling functions such as DisposePanel in the LabWindows/CVI Runtime. On the other hand, as explained in the preceding section, it is generally unsafe to call into other DLLs in response to the PROCESS_DETACH message.

To solve this dilemma, you can use the CVIRTEHasBeenDetached function in the Utility Library. It is always safe to call the CVIRTEHasBeenDetached function. CVIRTEHasBeenDetached returns FALSE until the main runtime DLL, cvirte.dll, receives the PROCESS_DETACH message. Consequently, if CVIRTEHasBeenDetached returns FALSE, your DLL can safely call functions in the LabWindows/CVI Runtime to release resources.

Note  cvirte.dll contains the User Interface, Utility, Formatting and I/O, RS-232, ANSI C, TCP Support, and DDE Support Libraries.