Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/255625

From BetaArchive Wiki

Article ID: 255625

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 Q255625

SYMPTOMS

After an object raises a trappable error, the client checks the Err object and finds that the error number is zero and the error description is empty.

CAUSE

This problem occurs only when the client application is compiled to native code and when the client does not have a local or global variable that stores a reference to the object that raises the error.

Assume that you have an object called Object1 and it has a property called ChildObject that returns a reference to another object, Object2. If you call a method on Object2 using a syntax such as:

Object1.ChildObject.Method
                

you see this type of behavior because no variable holds a reference to Object2.

RESOLUTION

The workaround for this problem is to assign an object to a variable and avoid using the preceding syntax. Do the following:

Dim MyObject as Object2

Set MyObject = Object1.ChildObject
' call the method using the MyObject variable
MyObject.Method

                

Another workaround is to compile your project to p-code.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new ActiveX DLL project. Class1 is created by default.
  2. From the Project menu, select Project1 Properties, and then set the name to MyDll.
  3. From the Project menu, add a new class module to this project. This is called Class2 by default.
  4. Paste the following code into the Class1 code module:

    Option Explicit
    Public Sub DoSomething
        Err.Raise 101
    End Sub
    
    Private Sub Class_Terminate
        Dim i as Long
    
        ' if you comment the following line, the error object is not reset
        On Error Resume Next
        
        i=0
    End Sub
                        
  5. Paste the following code into the Class2 code module:

    Option Explicit
    Public Property Get ChildObject() As Class1
        Set ChildObject = New Class1
    End Property
                        
  6. Save the project and compile it as MyDll.Dll.
  7. Start a new Standard EXE project. Form 1 is created by default.
  8. From the Project menu, select References, select MyDll, and then click OK to add a reference to the DLL you just created.
  9. Add a CommandButton to Form1. This is Command1 by default.
  10. Paste the following code into Form1's code module:

    Option Explicit
    Private Sub Command1_Click()
       Dim MyObj As New MyDll.Class2
       On Error GoTo p1
       MyObj.ChildObject.DoSomething
       Exit Sub
    p1:
       MsgBox "Err.Number=" & Err.Number
    End Sub
                        
  11. From the Project menu, select Project1 Properties, and then select the Compile tab. Select Compile to P-Code.
  12. From the File menu, select Make to compile the Standard EXE project to p-code. Run the executable and click on Command1. Note that you get a message box with "Err.Number=101" as expected.
  13. Dismiss the message box and stop the project to return to the design environment.
  14. From the Project menu, select Project1 Properties, and select the Compile tab. Select Compile to Native Code.
  15. From the File menu, select Make to compile the project to native code. Run the executable and click on Command1. Note that you get a message box with "Err.Number=0" which is not as expected. The problem is caused by the Class_Terminate event. When you use the syntax Object1.ChildObject.Method, Object2 gets released before the next line of code is executed, which triggers the Class_Terminate event, and the Err object is reset in the terminate event procedure. If you comment out the line that uses the Err object in this event, the problem does not happen.


Keywords: kbbug kbnofix KB255625