Attempted to read or write protected memory

4609
2
08-22-2012 01:22 PM
SpencerGardner
New Contributor II
I recently moved my C# .NET project from a Windows 7 box (x64) to an older Windows XP box (32-bit) and have since started receiving the following error:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


Here's a condensed code snippet:

IGeoProcessor2 gp = new GeoProcessorClass();
gp.SetEnvironmentValue("workspace", gdbLocation);
IVariantArray analysisParameters = new VarArrayClass();
[...add parameter values to array...]
gp.Execute("Pm1", analysisParameters, null);


Looking through the forums reveals a wide range of potential issues. One helpful suggestion was to do garbage collection before the offending call, so I modified my code to this:

IGeoProcessor2 gp = new GeoProcessorClass();
gp.SetEnvironmentValue("workspace", gdbLocation);
IVariantArray analysisParameters = new VarArrayClass();
[...add parameter values to array...]
GC.Collect();
GC.WaitForPendingFinalizers();
gp.Execute("Pm1", analysisParameters, null);


Now I am getting a COM error on GC.WaitForPendingFinalizers(). The message reads:
An exception was caught but handled while releasing a COM interface pointer through Marshal.Release or Marshal.ReleaseComObject or implicitly after the corresponding RuntimeCallableWrapper was garbage collected. This is the result of a user refcount error or other problem with a COM object's Release. Make sure refcounts are managed properly.  The COM interface pointer's original vtable pointer was 0x1b8d9110. While these types of exceptions are caught by the CLR, they can still lead to corruption and data loss so if possible the issue causing the exception should be addressed


I am not familiar enough with the inner workings of COM to be able to troubleshoot this issue any further. What exactly is the error saying? It appears that the base issue is related to the interoperation of .NET and COM, which is out of my control. What can I do to circumvent this problem?
0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
Is your tool trying to overwrite an existing dataset? If so, have you set the OverwriteOutput to true?

In the geoprocessing tools I've written for VB.NET, I use ComReleaser as in this example.

    Friend Function DeleteDataset(ByVal Input As Object) As Boolean

        Dim DeleteDS As New ESRI.ArcGIS.DataManagementTools.Delete
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(DeleteDS)

                DeleteDS.in_data = Input

                Result = RunTool(DeleteDS, Nothing, False)
                If Result Is Nothing Then Return False

                Return True

            End Using
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Delete Dataset")
            Return False
        End Try

    End Function
0 Kudos
SpencerGardner
New Contributor II
Nope. No overwriting or deleting going on.
0 Kudos