"System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt" when using cmAddShape and cmEditshape

hello everyone. I am using shapeeditor to draw some types as points, polylines, polygons on my map. When using current zoom small less level 9, my map run common, however, when I zoom it up to larger than 10, I assign mouse to cmAddShape
(ex: axMap.CursorMode = tkCursorMode.cmNone; axMap.CursorMode = CursorMode.cmAddShape;). My program occurs error following: “System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt”. I can’t found why it happended.
Please recommend me some idea to fix it.
Thanks a lot.

Hello @tiennguyen

  1. What version of the OCX are you using?
  2. Could you post more specific code? Then we may be able to diagnose or reproduce the problem.

Thank you.
Jerry.

thanks for your answer. I am using MapWinGis 5.1.1 and my code write as follow:
try
{
Shapefile sf = axMap.get_Shapefile(idLayHndPoint);
if (sf == null)
return;

            if (axMap.CursorMode != tkCursorMode.cmAddShape)
            {
                
                sf.InteractiveEditing = true; 
                drawLayer = (int)clsConst.ENUM_DRAW_LAYER.SHP_POINT;

                axMap.CursorMode = tkCursorMode.cmNone;
                axMap.CursorMode = tkCursorMode.cmAddShape;
                GlobVar.mouseFlag = GlobVar.MOUSE_FLAG.MOUSE_DRAW_POINT;                    

                addPoint.Checked = true;

                addPolyline.Enabled = false;
                addPolygon.Enabled = false;
                addCircle.Enabled = false;
                editShape.Enabled = false; 

                //addPolyline.Checked = false;
                //addPolygon.Checked = false;
                //addCircle.Checked = false;

                ZoomInLevel.Checked = false;
                ZoomOutLevel.Checked = false;
                MeasureBtn.Checked = false;
                MeasureAreaBtn.Checked = false;
                editShape.Checked = false;
            }
            else
            {
                drawLayer = (int)clsConst.ENUM_DRAW_LAYER.NONE;
                axMap.CursorMode = tkCursorMode.cmNone;
                GlobVar.mouseFlag = GlobVar.MOUSE_FLAG.MOUSE_NONE;

               // sf.Close();                     
               // axMap.ShapeEditor.RawData.Clear(); 
                addPoint.Checked = false;
                addPolyline.Enabled = true;
                addPolygon.Enabled = true;
                addCircle.Enabled = true;
                editShape.Enabled = true; 
            }

        }
        catch (Exception ex)
        {
            clsCommon.glogError.Fatal(ex.ToString()); 
            throw;
        }
    }

File shapefile is created in advance and choose layer was written as follow:
try
{
//1. Add shape operation
if (axMap.CursorMode == tkCursorMode.cmAddShape)
{
if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.NONE) // return
return;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.WARFARE_AREA)
e.layerHandle = idLayHndWFArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.GEO_AREA)
e.layerHandle = idLayHndGeoArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SEA_AREA)
e.layerHandle = idLayHndSeaArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.AIR_AREA)
e.layerHandle = idLayHndAirArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SHP_POINT)
e.layerHandle = idLayHndPoint;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SHP_POLYLINE)
e.layerHandle = idLayHndPolyline;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SHP_POLYGON)
e.layerHandle = idLayHndPolygon;
}
//2. select object by cmSelection
if (axMap.CursorMode == tkCursorMode.cmMoveShapes)
{
e.layerHandle = idLayHndWFArea;
}
//if (axMap.CursorMode == tkCursorMode.cmEditShape)
//{

            //    e.layerHandle = idLayHndCircle; 
            //    double tmpLat = e.yProj;
            //    double tmpLon = e.xProj; 
            //}
        }
        catch (Exception ex)
        {
            clsCommon.glogError.Fatal(ex.ToString()); 
            throw;
        }

Hi. I think you do not understand my explanation. Please allow me explain it again for you. I hope you might tell me some idea to repair it.
Target:
I am using some advantages of shapeEditor class to draw something, in which I assign axMap.CursorMode = cmAddShape to draw points, polylines, polygons and all of them is saved into shapefile.
Implementation:
To implement it I wore code as follow:
step1: I create some shapefile (.shp) to save. This function was called in Load of my application.

            LoadShpGeoFile(ref sfPoint, "sf_Points.shp", ref idLayHndPoint);
            LoadShpGeoFile(ref sfPolyline, "sf_Polyline.shp", ref idLayHndPolyline);
            LoadShpGeoFile(ref sfPolygon, "sf_Polygons.shp", ref idLayHndPolygon);
            LoadShpGeoFile(ref sfCircle, "sf_Circles.shp", ref idLayHndCircle);

In each LoadShpGeoFile(), I create a file .shp with type point, polygon, polyline respectively. Furthermore I configure options for file, for instance, color, linewidth, … . Finnally I add .shp file to map and save index of layerHandle which return by function AddLayer () ( to use for step 3). you can see attacted file.
Step2: When user select a button on toolbar, I set flag for mouse:
private void addPoint_Click(object sender, EventArgs e)
{
try
{
Shapefile sf = axMap.get_Shapefile(idLayHndPoint);
if (sf == null)
return;

            if (axMap.CursorMode != tkCursorMode.cmAddShape)
            {
                
                sf.InteractiveEditing = true; 
                drawLayer = (int)clsConst.ENUM_DRAW_LAYER.SHP_POINT;

                axMap.CursorMode = tkCursorMode.cmNone;
                axMap.CursorMode = tkCursorMode.cmAddShape;
                GlobVar.mouseFlag = GlobVar.MOUSE_FLAG.MOUSE_DRAW_POINT;                    

                addPoint.Checked = true;

                addPolyline.Enabled = false;
                addPolygon.Enabled = false;
                addCircle.Enabled = false;
                editShape.Enabled = false; 

                //addPolyline.Checked = false;
                //addPolygon.Checked = false;
                //addCircle.Checked = false;

                ZoomInLevel.Checked = false;
                ZoomOutLevel.Checked = false;
                MeasureBtn.Checked = false;
                MeasureAreaBtn.Checked = false;
                editShape.Checked = false;
            }
            else
            {
                drawLayer = (int)clsConst.ENUM_DRAW_LAYER.NONE;
                axMap.CursorMode = tkCursorMode.cmNone;
                GlobVar.mouseFlag = GlobVar.MOUSE_FLAG.MOUSE_NONE;

               // sf.Close();                     
               // axMap.ShapeEditor.RawData.Clear(); 
                addPoint.Checked = false;
                addPolyline.Enabled = true;
                addPolygon.Enabled = true;
                addCircle.Enabled = true;
                editShape.Enabled = true; 
            }

        }
        catch (Exception ex)
        {
            clsCommon.glogError.Fatal(ex.ToString());
            MessageBox.Show(ex.ToString()); 
            throw;
        }
    }

Step3: In ChooseLayer Event, I only assign e.LayerHandle = id which save in step1.
try
{
//1. Add shape operation
if (axMap.CursorMode == tkCursorMode.cmAddShape)
{
if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.NONE) // return
return;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.WARFARE_AREA)
e.layerHandle = idLayHndWFArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.GEO_AREA)
e.layerHandle = idLayHndGeoArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SEA_AREA)
e.layerHandle = idLayHndSeaArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.AIR_AREA)
e.layerHandle = idLayHndAirArea;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SHP_POINT)
e.layerHandle = idLayHndPoint;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SHP_POLYLINE)
e.layerHandle = idLayHndPolyline;
else if (drawLayer == (int)clsConst.ENUM_DRAW_LAYER.SHP_POLYGON)
e.layerHandle = idLayHndPolygon;
else
e.layerHandle = idLay;
}
//2. select object by cmSelection
if (axMap.CursorMode == tkCursorMode.cmMoveShapes)
{
e.layerHandle = idLayHndWFArea;
}
//if (axMap.CursorMode == tkCursorMode.cmEditShape)
//{

            //    e.layerHandle = idLayHndCircle; 
            //    double tmpLat = e.yProj;
            //    double tmpLon = e.xProj; 
            //}
        }
        catch (Exception ex)
        {
            clsCommon.glogError.Fatal(ex.ToString());
            MessageBox.Show(ex.ToString()); 
            throw;
        }
        
    }

My problem:
when my application zoom map not upto 9 level (current zoom <=9) , i choose cmAddShape to draw, it operate perfectly. However, when I zoom map over 10 and i choose mouse mode to draw point, polyline, polygon by cmAddShape. It occurs an error which try {} catch of c# not capture it.

[“System.AccessViolationException: ‘Attempted to read or write protected memory. This is often an indication that other memory is corrupt”]. Furthermore, in output window of visual studio 2017 enterprise, I see a report that content:

Exception thrown at 0x775343D2 in C2Console.exe: Microsoft C++ exception: geos::util::IllegalArgumentException at memory location 0x00AFDDF4.
Exception thrown at 0x775343D2 in C2Console.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
Exception thrown at 0x60C6DD25 (geos_c.dll) in C2Console.exe: 0xC0000005: Access violation reading location 0x00000000.
Exception thrown: ‘System.AccessViolationException’ in System.Windows.Forms.dll
I really have not found a method to solve it.
Can you help me?
Thans a lot. myCode.zip (2.7 KB)

hj jeryfaust.
Can you help me?

Hello @tiennguyen

I would like to help you if I can. I don’t see anything after initial review. Instead, I would have to reproduce the problem by running the code, to see where the exception is occurring (in the OCX). But to do that, I need a complete application. There are too many missing pieces in the code sample you sent.

Is it possible to create a smaller (scaled down) sample program that reproduces the problem, and send me the code for that? I can then debug the OCX and hopefully find the exception.

Thank you,
Jerry.