The procedures in this topic illustrate the key events in the lifetime of an object provided by an in-process component, including what happens when the DLL is unloaded forcefully by clicking the End button.
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 run the TestThing test application
Note By default the Compile On Demand option is checked on the General tab of the Options dialog box (accessed from the Tools menu). When you’re debugging a component with Compile On Demand checked, you may find it useful to use CTRL+F5 (or Start with Full Compile, on the Run menu) to compile all the projects in the group before entering run mode. Compile errors usually require resetting the project, which means returning to design mode.
When the Command1_Click event procedure creates the test Thing object, two things happen. First, before the object is created, the code in Sub Main
is executed. Only after Sub Main
executes is the Thing object created.
Notice that the Name property has no value yet.
Important The Sub Main procedure of an ActiveX component is executed when the component receives the first request for one of the objects it provides, before the component creates the object. You should not put lengthy tasks in the Sub Main procedure, because a request to create an object may time out while waiting for Sub Main
to execute. This is discussed in "Starting and Ending a Component" in "General Principles of Component Design."
In the Immediate window, you will see a debug message from the Initialize event of the new Thing, as the New operator causes the object to be created. This is followed by the Terminate message from the original Thing, the one you named First Thing.
The original Thing object is destroyed when the reference to the new Thing object is placed in the variable mthTest
. At that point, there are no variables holding references to the original Thing, so it must be destroyed.
Before the program closes, the Terminate message for the Thing object you named Second Thing is displayed in the Immediate window.
When you close a program, Visual Basic cleans up all the variables that still contain object references. As each variable is set to Nothing, the object it referred to is destroyed.
When a program is ended abruptly, with the End button or by an End statement in code, Visual Basic reclaims any memory and resources the program was using. However, the cleanup is done as if the program had suffered a fatal error. Objects will not get their Terminate events.
To observe the effects of the End button
Important Remember that ending your program with the End button, or with an End statement in your code, halts the program immediately, without executing the Terminate events of any objects. It’s always better to shut your program down by unloading all the forms.
For More Information The rules of object lifetime are introduced in "Programming with Objects" in the Visual Basic Programmer’s Guide.
This topic is part of a series that walks you through creating a sample ActiveX DLL.
To | See |
Go to the next step | Circular References and Object Lifetime |
Start from the beginning | Creating an ActiveX DLL. |