DOCUMENT:Q173351 03-SEP-1997 [vbwin] TITLE :BUG: Custom OCX Not Unloaded When Client Application Ends PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:5.0 OPER/SYS:WINDOWS KEYWORDS:vb5all ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0 --------------------------------------------------------------------- SYMPTOMS ======== If a variable is set to reference a UserControl or one of its constituent controls in the InitProperties and ReadProperties events of the UserControl, the reference is not released when the client application ends; therefore, the OCX remains loaded. Because the OCX does not unload, its Terminate event is not triggered when the application ends. RESOLUTION ========== You can work around this problem by using a variable that saves the Name property of the control rather than an actual reference to the control. For example, if you are using the following code in a UserControl in which the Terminate event is not triggered: Dim o As Object Private Sub UserControl_InitProperties() Set o = Label1 End Sub Private Sub UserControl_ReadProperties(PropBag As PropertyBag) Set o = Label2 End Sub replace the code with the following: Dim s As String Private Sub UserControl_InitProperties() s = Label1.Name End Sub Private Sub UserControl_ReadProperties(PropBag As PropertyBag) s = Label2.Name End Sub STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available. MORE INFORMATION ================ Steps to Reproduce Behavior --------------------------- 1. Start a new "ActiveX Control" project. 2. Add two Label controls to UserControl1. 3. Add the following code to UserControl1: Option Explicit Dim o As Object Private Sub UserControl_InitProperties() Set o = Label1 MsgBox "InitProperties" End Sub Private Sub UserControl_ReadProperties(PropBag As PropertyBag) Set o = Label2 MsgBox "ReadProperties" End Sub Private Sub UserControl_Terminate() MsgBox "Terminate" End Sub 4. Save the project and Make the OCX. 5. Start a new "Standard EXE" project. 6. Click Components on the Project menu. Check the custom OCX you built in step 4. 7. Add the custom control to Form1. Note that the InitProperties event fires as expected. 8. Press the F5 key to run the program. Note that the ReadProperties event fires. However, you would expect to receive a Terminate event and then the ReadProperties event. 9. Click End on the Run menu. Note that only ReadProperties fires, not Terminate and then ReadProperties as expected. 10. Exit the IDE. Note that the OCX now unloads and the Terminate events are triggered. ====================================================================== Keywords : vb5all Version : WINDOWS:5.0 Platform : WINDOWS Issue type : kbbug ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1997.