I'm getting an error when my program shuts down, now that I have added lots of layers from the WorldMapKit. I had never used shapefile layers before, so I don't know if that is the issue or not.
The error is because the Proj4 projection.Free method is trying to access memory that has likely already been freed (called corrupt). I have added a "Terminate" routine to my app to try and close/free all of the projections manually, rather than letting the Garbage Collector do it, but this still fails:
Here is the error:
System.Reflection.TargetInvocationException was unhandled
Message="Exception has been thrown by the target of an invocation."
Source="mscorlib"
StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at ThinkGeo.MapSuite.Core.Proj4Projection.xebc36fa7da52d8d7(IntPtr x4fa9e7eeb5ea7ffa)
at ThinkGeo.MapSuite.Core.Proj4Projection.CloseCore()
at ThinkGeo.MapSuite.Core.Projection.Close()
at ThinkGeo.MapSuite.Core.Proj4Projection.x0f09c3fcb8980efd(Boolean x8d14c6905e5eb7a3)
at ThinkGeo.MapSuite.Core.Proj4Projection.Finalize()
InnerException: System.AccessViolationException
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="Proj4ExtensionX86"
StackTrace:
at Proj4Extension.Proj4Extensionx86.pj_free(IntPtr projPJ)
at Proj4Extension.Proj4Extensionx86.Free(IntPtr projPJ)
InnerException:
Here is the code:
public void Terminate() { _tigerOverlay.Lock.EnterWriteLock(); foreach (FeatureLayer fl in _tigerOverlay.Layers) { try { fl.FeatureSource.Projection.Close(); // If I leave this line in place, the above error is thrown // when the second layer is closed (and on all subsequent layers). // If I comment this line out, the error does not occur until the // main application has exited. fl.FeatureSource.Projection = null; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } // As an aside, I tried to remove the layers one at a time // and this method fails (I was in a while loop, not a for loop). //_tigerOverlay.Layers.Remove(fl); } _tigerOverlay.Layers.Clear(); _tigerOverlay.Lock.ExitWriteLock(); }
Please reference the comments for other things I also believe to be an issue. However, it appears to me that when I create three different projections, that they might all reference the same unmanaged projection or chunck of memory? And when any one of of the projections are set to null, that object or chunk of memory that is shared by all identical projections is destroyed, generating the corrupt memory error when subsequent projections are closed.