English

Error: The instruction at "0xxxxxxx" referenced could not be read

Error Message

Engine and Desktop applications, written in Visual Studio .NET, may experience crashes upon exiting. The following error message is displayed:

"The instruction at "0xxxxxx" referenced could not be read."

Cause

The crashes result from COM objects being loaded longer than expected.

Solution or Workaround

  • To stop the crash, all COM references must be unloaded prior to shutdown. To help unload COM references, a special static Shutdown function has been added to the ESRI.ArcGIS.Utility assembly. The following code excerpt shows the function in use.

    Code:
    [Visual Basic .NET]
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown()
    End Sub

    [C#]
    private void Form1_Closing(object sender, CancelEventArgs e)
    {
    ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown();
    }

  • The AOUninitalize.Shutdown function handles most of the shutdown problems in standalone applications, but problems may still be experienced due to COM objects that additionally require explicit releasing. In these cases, call System.Runtime.InteropServices.Marshal.ReleaseComObject() to decrement the reference count, which allows the application to terminate cleanly.

    The StyleGallery is one such object and the following example documents how to handle references to this class.
    Code:

    [Visual Basic .NET]
    Dim styleGallery As IStyleGallery
    styleGallery = New StyleGalleryClass
    MessageBox.Show(styleGallery.ClassCount)
    Marshal.ReleaseComObject(styleGallery)
    [C#]
    IStyleGallery sg = new StyleGalleryClass() as IStyleGallery;
    MessageBox.Show(sg.ClassCount.ToString());
    Marshal.ReleaseComObject(sg);