Microsoft KB Archive/237757

= INFO: Leaking Handles When Creating COM Objects =

Article ID: 237757

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q237757



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 or Windows 2000:  Create a Visual Basic ActiveX EXE project. Class1 is created by default. On the Project menu, click Project1 Properties. Set the contents of the Project Name field to MyServer.  Add the following Code to the General Declarations section of Class1: Public Function GetLong GetLong=1 End Function  Save the project and compile it. Create a new Visual Basic Standard EXE project. Form1 is created by default. From the Project menu, select References and add a reference to the server you just created. Place a CommandButton on Form1.</li> Place a label beside the button and clear the Caption property.</li>  Add the following code to the General Declarations section of Form1: 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 </li> Save the project and compile it.</li> Close the Visual Basic Design Environment (IDE).</li> Open the NT or Windows 2000 task manager and click on the Performance tab.</li> Run the compiled client.</li> Click the button and observe the number of handles. It will grow and will not come back down after creating the objects.</li> Wait until the label displays "Done," indicating all objects were created.</li> 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. (This does not occur in Windows 2000.)</li> 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.</li></ol>

Keywords: kbinfo KB237757

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.