none
Interop problem + com object that has been separated from its underlying rcw cannot be used RRS feed

  • Question

  • Hi,

    I have an desktop application in VB .net calling Native OCX using Interop.

     

    Application is simple.

    For one textbox1 I have captured  GotFocus and LostFocus events

    and on other textbox2 I have done nothing.

    code is like that

     Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus

            OCXObj.Enable()

        End Sub

        Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus      

            OCXObj.Disable()

        End Sub

     

    But on closing of application,application get crashed  when focus is on textbox1.

    ERROR  "com object that has been separated from its underlying rcw cannot be used"

     

    What I observe is as follows:

    1.Focus on textbox1, onclick of close button LostFocus for text box is called and crashed.I observe OBJECT of ocx get crashed inside function OCXObj.Disable().

    2.Focus on textbox1,now clicked on textbox2 same LostFocus  event get called but no crash  and now close application , successfully window get closed.

     

    So In 1st case what went wrong while closing,I got no idea and error "com object that has been separated from its underlying rcw cannot be used"

    Thursday, February 24, 2011 2:25 PM

Answers

  • Hello Hummer4WD,

     

    1. >> ERROR  "com object that has been separated from its underlying rcw cannot be used"

    1.1 All COM objects including OCXs are accessed in managed code via Runtime-Callable-Wrappers (RCWs).

    1.2 This exception is thrown when a method or property of an OCX is accessed but the RCW for the OCX has already released the underlying OCX object.

     

    2. The release normally occurs during the execution of the containing Form's IDisposable.Dispose() method when the application terminates :

    Partial Class Form1
        Inherits System.Windows.Forms.Form

        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

    2.1 Upon calling "MyBase.Dispose(disposing)", the OCX is released by its RCW.

    2.2 Note the crucial sequence of events :

    2.2.1 Cursor is on textbox1 hence textbox1 has focus.

    2.2.2 Application terminates.

    2.2.3 Form's Dispose() method is called. RCX releases OCX.

    2.2.4 textbox1 LostFocus() event called, the OCX's Disable() method is called but the RCW has already released its OCX. Hence the exception.

     

    3. One way to resolve this issue is to insert a check on the containing Form's Disposing property inside the LostFocus() event of textbox1, e.g. :

        Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
            If (Me.Disposing = False) Then
                '' The containing form of OCXObj is not in the midst of
                '' disposing. So OCXObj is usable.
                OCXObj.Disable()
            End If
        End Sub

    3.1 If the containing form is in the middle of performing Dispose(), the Disposing property is true. Call "OCXObj.Disable()" only when Disposing is false.

     

    Hope the above will be helpful.

    - Bio.

     

    • Marked as answer by Hummer4WD Friday, February 25, 2011 9:00 AM
    Friday, February 25, 2011 5:39 AM

All replies

  • Hello Hummer4WD,

     

    1. >> ERROR  "com object that has been separated from its underlying rcw cannot be used"

    1.1 All COM objects including OCXs are accessed in managed code via Runtime-Callable-Wrappers (RCWs).

    1.2 This exception is thrown when a method or property of an OCX is accessed but the RCW for the OCX has already released the underlying OCX object.

     

    2. The release normally occurs during the execution of the containing Form's IDisposable.Dispose() method when the application terminates :

    Partial Class Form1
        Inherits System.Windows.Forms.Form

        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

    2.1 Upon calling "MyBase.Dispose(disposing)", the OCX is released by its RCW.

    2.2 Note the crucial sequence of events :

    2.2.1 Cursor is on textbox1 hence textbox1 has focus.

    2.2.2 Application terminates.

    2.2.3 Form's Dispose() method is called. RCX releases OCX.

    2.2.4 textbox1 LostFocus() event called, the OCX's Disable() method is called but the RCW has already released its OCX. Hence the exception.

     

    3. One way to resolve this issue is to insert a check on the containing Form's Disposing property inside the LostFocus() event of textbox1, e.g. :

        Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
            If (Me.Disposing = False) Then
                '' The containing form of OCXObj is not in the midst of
                '' disposing. So OCXObj is usable.
                OCXObj.Disable()
            End If
        End Sub

    3.1 If the containing form is in the middle of performing Dispose(), the Disposing property is true. Call "OCXObj.Disable()" only when Disposing is false.

     

    Hope the above will be helpful.

    - Bio.

     

    • Marked as answer by Hummer4WD Friday, February 25, 2011 9:00 AM
    Friday, February 25, 2011 5:39 AM
  • Thanks,Reply is really helpful
    Friday, February 25, 2011 9:16 AM
  • Hi Lim,

     

    Glad to see you again.

     

    You are such a helpful member in this forum.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 28, 2011 2:17 AM
  • Hello Paul,

    >> You are such a helpful member in this forum.

    That's very kind, Paul. Thanks very much. I'm always glad to be of help :-)

    Best Regards,

    Bio.

     

    Monday, February 28, 2011 12:36 PM