INFO: Leaking Handles When Creating COM Objects

ID: Q237757


The information in this article applies to:


SUMMARY

When a Visual Basic application creates COM objects that are instantiated in an out-of-process server, observe the number of handles shown in the performance tab of the NT task manager. You might see the number of handles increasing, without returning to its initial level when the objects are released.

Although this behavior appears to be a leak, it is not. This behavior is by design.


MORE INFORMATION

Visual Basic does not release the handles it creates immediately after it uses them; it caches them. When the number of handles reaches a given level, they are released and new allocations start again when they are needed. All handles are recovered when the client application is closed.

This is the expected behavior. Visual Basic is just optimizing the creation/release of handles internally.

You can observe this behavior by doing the following under Windows NT:

  1. Create a Visual Basic ActiveX EXE project. Class1 is created by default.


  2. On the Project menu, click Project1 Properties. Set the contents of the Project Name field to MyServer.


  3. Add the following Code to the General Declarations section of Class1:


  4. 
    Public Function GetLong()
        GetLong=1
    End Function 
  5. Save the project and compile it.


  6. Create a new Visual Basic Standard EXE project. Form1 is created by default.


  7. From the Project menu, select References and add a reference to the server you just created.


  8. Place a CommandButton on Form1.


  9. Place a label beside the button and clear the Caption property.


  10. Add the following code to the General Declarations section of Form1:


  11. 
    Private Sub Command1_Click()
       Dim i As Long
       Dim MyObj As MyServer.Class1
       Label1.Caption = "Creating..."
       Form1.Refresh
       For i = 1 To 200
          Set MyObj = CreateObject("MyServer.Class1")
          Set MyObj = Nothing
       Next i
       Label1.Caption = "Done"
    End Sub 
  12. Save the project and compile it.


  13. Close the Visual Basic Design Environment (IDE).


  14. Open the NT task manager and click on the performance tab.


  15. Run the compiled client.


  16. Click the button and observe the number of handles. It will grow and will not come back down after creating the objects.


  17. Wait until the label displays "Done," indicating all objects were created.


  18. Repeat this by clicking the button several times. You will see that after the third or fourth time the number of handles drops to a lower value.


  19. You could also do the same with 1000 iterations, by changing the code in the button's click event (for i=1 to 1000) and observe the number of handles while these 1000 objects get created. You should see the number of handles going up and down.


Additional query words:


Keywords          : kbActiveX kbCOMt kbVBp600 kbGrpVB kbDSupport 
Version           : WINDOWS:6.0
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: August 12, 1999