0

I am currently developing using ArcObects 10.2.1 and I've encountered this tricky exception:

AccessViolationException was encountered

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Source=System.Windows.Forms

StackTrace:

   System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   System.Windows.Forms.Control.DefWndProc(Message& m)
   System.Windows.Forms.Control.WndProc(Message& m)
   System.Windows.Forms.AxHost.WndProc(Message& m)
   System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

InnerException:

I tried to annotate the code and find out where it was wrong,I found it's in the last line.but I still don't know how to solve it.

    public void SetSimpleLabel(IFeatureLayer featLayer, string field, string where = "")
    {
        IGeoFeatureLayer geoFeatureLayer = featLayer as IGeoFeatureLayer;
        IAnnotateLayerPropertiesCollection annoProperties = geoFeatureLayer.AnnotationProperties;
        annoProperties.Clear();

        ILineLabelPosition position = new LineLabelPosition();
        position.Parallel = true;
        position.Perpendicular = false;

        ILineLabelPlacementPriorities placement = new LineLabelPlacementPriorities();
        IBasicOverposterLayerProperties basic = new BasicOverposterLayerProperties();
        basic.FeatureType = esriBasicOverposterFeatureType.esriOverposterPoint;
        basic.LineLabelPlacementPriorities = placement;
        basic.LineLabelPosition = position;

        ILabelEngineLayerProperties labelProperties = new LabelEngineLayerProperties() as ILabelEngineLayerProperties;
        labelProperties.Symbol = sf.CreateTextSymbol(null, 12, null, null, esriTextHorizontalAlignment.esriTHACenter);
        labelProperties.BasicOverposterLayerProperties = basic;
        labelProperties.Expression = "[" + field + "]";
        IAnnotateLayerProperties annoLayerProperties = labelProperties as IAnnotateLayerProperties;
        if (where != "")
            annoLayerProperties.WhereClause = where;
        annoProperties.Add(annoLayerProperties);

        geoFeatureLayer.DisplayAnnotation = true; //it cause
    }

why does it cause the exception?Does anybody have any ideas?

  • 1
    Few hundred things: wrong P/Invoke signature. Messing something with pointers in your code. Playing with an invalid form/gdi/gdi+ resource handle. Bug in their code (which may also be a consequence of wrong usage). Note that that specific line of code may exploit the error but it may be (or it may not be) the real cause. Imagine somewhere else you overwrote a data structure (because of one of the above listed reasons) and that data structure is used if you set that property to true... – Adriano Repetti Jun 21 '17 at 09:20
  • @AdrianoRepetti I found the problem and solved it.There's no problem with this code. The reason is that the Annotation must use AxMapControl, but I only use AxPageLayoutControl. Thank you very much for providing a good idea. – Helen Wang Jun 23 '17 at 02:31

0 Answers0