I am using SharpMap within a window service. I am using the library to generate a map and then create an image out of it and add it to an Excel file. The code runs within a Quartz.net task.
The error happens when I call SharpMap.Map.GetMap()
and crushes my service (see error report and stack call below).
This only happens every now and then (maybe issues with data?). Also note, I had some issues with working with the map so I put some delay (System.Threading.Thread.Sleep(5000);
) just before calling the function.
I am generating several such images and the error (last time it occurred) was after a few (~10) iterations. In order not to recreate and format the "static layers" of the map (SharpMap.Map
) each time I just add and remove the "dynamic" layers and resize some features in each iteration.
Is there a way to find what the cause of the error?
Is there a way to gracefully handle such exception?
The Windows CMS error report + stack trace:
Application: NePTune.WinService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
at System.Drawing.SafeNativeMethods+Gdip.GdipPathIterNextSubpathPath(System.Runtime.InteropServices.HandleRef, Int32 ByRef, System.Runtime.InteropServices.HandleRef, Boolean ByRef)
at System.Drawing.SafeNativeMethods+Gdip.GdipPathIterNextSubpathPath(System.Runtime.InteropServices.HandleRef, Int32 ByRef, System.Runtime.InteropServices.HandleRef, Boolean ByRef)
at System.Drawing.Drawing2D.GraphicsPathIterator.NextSubpath(System.Drawing.Drawing2D.GraphicsPath, Boolean ByRef)
at SharpMap.Rendering.Symbolizer.WarpPathToPath.Warp(System.Drawing.Drawing2D.GraphicsPath, System.Drawing.Drawing2D.GraphicsPath, Boolean, Single)
at SharpMap.Rendering.Symbolizer.WarpPathToPath.DrawString(System.Drawing.Graphics, System.Drawing.Pen, System.Drawing.Brush, System.String, System.Drawing.FontFamily, Int32, Single, System.Drawing.StringFormat, Boolean, System.Drawing.Drawing2D.GraphicsPath)
at SharpMap.Layers.LabelLayer.Render(System.Drawing.Graphics, SharpMap.Map)
at SharpMap.Map.RenderMap(System.Drawing.Graphics)
at SharpMap.Map.GetMap()
at NePTune.Logic.Algorithms.UlInterference.UlInterferenceReportWriter.CreateClusterMap(System.Collections.Generic.List1<NePTune.Data.SectorGeoDetails>, System.Collections.Generic.Dictionary2<Int32,Double>, Int32, System.Collections.Generic.IEnumerable1<NePTune.Data.SectorGeoDetails>, Boolean)
at NePTune.Logic.Algorithms.UlInterference.UlInterferenceReportWriter.CreateInterferenceClusterWorksheets(OfficeOpenXml.ExcelPackage, System.Collections.Generic.List1<NePTune.Logic.Algorithms.UlInterference.UlInterferenceCluster>, System.Collections.Generic.Dictionary2<Int32,NePTune.Data.SectorGeoDetails>, System.Collections.Generic.Dictionary2<Int32,NePTune.Logic.Algorithms.UlInterference.CellMeasurementList>)
at NePTune.Logic.Algorithms.UlInterference.UlInterferenceReportWriter.CreateReport(System.Collections.Generic.List1<NePTune.Logic.Algorithms.UlInterference.CellUlInterferenceResults>, System.Collections.Generic.List1<NePTune.Logic.Algorithms.UlInterference.UlInterferenceCluster>, System.Collections.Generic.Dictionary2<Int32,NePTune.Data.SectorGeoDetails>, System.Collections.Generic.Dictionary2<Int32,NePTune.Logic.Algorithms.UlInterference.CellMeasurementList>)
at NePTune.Logic.Algorithms.UlInterference.UlInterferenceDriver.Execute(NePTune.Common.Configuration.GlobalConfiguration, NePTune.Logic.Algorithms.UlInterference.UlInterferenceConfiguration)
at NePTune.Logic.Algorithms.AlgorithmJobs.UlInterferenceJob.Execute(NePTune.Common.Configuration.GlobalConfiguration, NePTune.Logic.Algorithms.Common.IAlgorithmConfiguration, System.String, System.String, System.String)
at NePTune.Logic.Algorithms.AlgorithmJobs.UlInterferenceJob.Execute(Quartz.IJobExecutionContext)
at Quartz.Core.JobRunShell.Run()
at Quartz.Simpl.SimpleThreadPool+WorkerThread.Run()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
I was discussing this in the SharpMap project page on CodePlex (https://sharpmap.codeplex.com/) and over there they suggested to clone the map before calling to get map.
I changed the code a little to make a clone of the map in each iteration before adding my "dynamic layers" and then calling
GetMap()
.This prevents the access violation.
See more details here: https://sharpmap.codeplex.com/discussions/558218