ThinkGeo.com    |     Documentation    |     Premium Support

Target of invocation error

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.


 



I changed my code to try and use the same projection for all of my layers, and I get a different error in the projection engine: 
  
 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.x95c4ca6342aad2f1(IntPtr xce536087faba9488, IntPtr x6d3e0b22c9356db3, Int64 x971ce686c8b74ac9, Double[] x31d531fc882737ba, Double[] x272f87fcdd19be88, Double[] xb5f5ca13f19c3e64) 
        at ThinkGeo.MapSuite.Core.Proj4Projection.ConvertToInternalProjectionCore(Double[] x, Double[] y) 
        at ThinkGeo.MapSuite.Core.Projection.ConvertToInternalProjection(RectangleShape rectangleShape) 
        at ThinkGeo.MapSuite.Core.FeatureSource.ConvertToInternalProjection(RectangleShape rectangle) 
        at ThinkGeo.MapSuite.Core.FeatureSource.GetFeaturesForDrawing(RectangleShape boundingBox, Double screenWidth, Double screenHeight, IEnumerable`1 returningColumnNames) 
        at ThinkGeo.MapSuite.Core.FeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers) 
        at ThinkGeo.MapSuite.Core.Layer.Draw(GeoCanvas canvas, Collection`1 labelsInAllLayers) 
        at ThinkGeo.MapSuite.DesktopEdition.LayerOverlay.DrawCore(GeoCanvas canvas) 
        at ThinkGeo.MapSuite.DesktopEdition.Overlay.Draw(GeoCanvas canvas) 
        at ThinkGeo.MapSuite.DesktopEdition.WinformsMap.x27d0591c2adda8d9(RectangleShape xb35a33b423b17f65, Overlay x99251f66cdabc2ad, Int32 xa209325f5c895f7e, Int32 x7454a0d1965919b1, GeographyUnit xbb704b4400ce6f76) 
        at ThinkGeo.MapSuite.DesktopEdition.WinformsMap.x27d0591c2adda8d9(Object x9c95f4613bdc3113) 
        at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state) 
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
        at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack) 
        at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state) 
   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_transform(IntPtr srcCs, IntPtr destCs, Int64 pointCount, Double[] x, Double[] y, Double[] z) 
             at Proj4Extension.Proj4Extensionx86.Transform(IntPtr srcCs, IntPtr destCs, Int64 pointCount, Double[] x, Double[] y, Double[] z) 
        InnerException:  


Hi,


I was having an issue with the projection also when I closed a shapefile layer.  Just creating a Proj4Projection when the app started and opening and closing it solved my issue.  You may want to try it also.


 


 



 var projection = new Proj4Projection();
            projection.InternalProjectionParametersString = Proj4Projection.GetEsriParametersString(ProjectionHelper.CreateGeoDeticWGS1984SystemReference());
            projection.ExternalProjectionParametersString = Proj4Projection.GetEsriParametersString(ProjectionHelper.CreateGeoDeticWGS1984SystemReference());
            projection.Open();
            projection.Close();


Thank you.    I just tried that, and it did not make a difference.    Actually, I think I’m kind of happy about that :)

Yeah, I don't like the solution either, but its what it took to get running.  ThinkGeo should add having a native .net projection library on their roadmap, it would be nice not to have to distribute and install a com based component for projection.



Thanks for your sharing, Brian!


Ted,
 
From the exception stack trace, it is possibly that the two problems should be caused by some same problem. It seems that when you close, the app, the projection is still running in another app but it is finalized in the main thread!
 
Also, I think it is same with following post, please take a look.
gis.thinkgeo.com/Support/DiscussionForums/tabid/143/aff/13/aft/5959/afv/topic/afpgj/1/Default.aspx#9530
 
Any ideas or information would be appreciated, let me know if you have any problems!
 
Thanks.
 
Yale