Visual Basic Concepts
The following procedure shows how Visual Basic unloads an in-process component after the client has released all references to its objects. It also shows how circular references can prevent this unloading, and highlights an important difference between public and private objects.
The procedure can only be run using the compiled component and compiled test project, because Visual Basic never unloads an in-process component project running in the development environment.
Note This topic is part of a series that walks you through creating a sample ActiveX DLL. It begins with the topic Creating an ActiveX DLL.
To observe DLL unloading
Private Sub Class_Terminate()
MsgBox "Private object destroyed"
End Sub
A private object cannot be created by clients, and should never be passed to them. As you’ll see, private objects do not keep in-process components from unloading, and a client that uses a reference to a private object after its component has unloaded will suffer a catastrophic program failure.
Option Explicit
Private mtt As TellTale ' New code.
Sub Main
Debug.Print "Executing Sub Main"
Set mtt = New TellTale ' New code.
End Sub
Once the projects are compiled, Debug statements can’t be used to show what’s happening internally. Like a canary in a coal mine, the TellTale object will give us an indication of what’s happening in the compiled component.
As you saw earlier, the temporary Thing doesn’t hang around long. The only reason to create it now is to load ThingDemo.dll and execute Sub Main. For this procedure, the interesting question is what happens when the Things are all gone.
What happened? There were no references to public objects, so after a decent interval (usually about two minutes, although this will vary depending on how frequently it gets idle time) Visual Basic will attempt to unload the in-process component.
When the DLL is unloaded, Visual Basic frees the memory it was using — including variables containing private object references. As a result the TellTale object is destroyed, and you get a visual cue that the DLL has unloaded.
If you create a new Thing object at this point, you’ll notice a slight pause as the DLL is reloaded.
Visual Basic can’t tell the difference between an internal reference to a public object and an external (client) reference to a public object, so it has no choice but to keep the DLL loaded.
For More Information The rules for component shutdown are listed in "Starting and Ending a Component" in "General Principles of Component Design," and discussed in more detail in "ActiveX Component Standards and Guidelines."
This topic is part of a series that walks you through creating a sample ActiveX DLL.
To | See |
Go to the next step | ActiveX DLL Creation Summary |
Start from the beginning | Creating an ActiveX DLL. |