spencerrecneps

Attempted to read or write protected memory

Discussion created by spencerrecneps on Aug 22, 2012
Latest reply on Aug 23, 2012 by spencerrecneps
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?

Outcomes