C# AccessViolationException[ArcEngine]

C# AccessViolationException[ArcEngine],c#,arcobjects,esri-arc-engine,C#,Arcobjects,Esri Arc Engine,我目前正在使用ArcObects 10.2.1进行开发,遇到了一个棘手的异常: 遇到AccessViolationException 试图读取或写入受保护的内存。这通常表示其他内存已损坏 Source=System.Windows.Forms 堆栈跟踪: System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr l

我目前正在使用ArcObects 10.2.1进行开发,遇到了一个棘手的异常:

遇到AccessViolationException

试图读取或写入受保护的内存。这通常表示其他内存已损坏

Source=System.Windows.Forms

堆栈跟踪:

   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)
内部异常:

我试着注释代码并找出错误的地方,我发现它在最后一行。但我仍然不知道如何解决它

    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
    }

为什么会出现异常?有人有什么想法吗?

几百件事:错误的p/Invoke签名。把代码中的指针弄乱了。正在使用无效的表单/gdi/gdi+资源句柄。代码中的错误(这也可能是错误使用的结果)。请注意,特定的代码行可能会利用该错误,但它可能是(也可能不是)真正的原因。假设您在其他地方重写了一个数据结构(由于上面列出的原因之一),并且如果您将该属性设置为true,则会使用该数据结构…@AdrianoRepetti我发现了问题并解决了它。此代码没有问题。原因是注释必须使用AxMapControl,但我只使用AxPageLayoutControl。非常感谢您提供了一个好主意。