0

我正在 VS2012 C# 中创建一个 ESRI ArcMap 10.2 扩展。我一直遇到一个问题,当我创建一个包含大量列的 shapefile 时,我得到了错误:

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

在功能工作区上实际运行 Create Feature 方法时。这是代码摘录:

public bool LoadData(DataQueryResult QueryResult, string ColumnX, string ColumnY)
{
    bool loaded = true;
    DataTable Data = QueryResult.ResultsTable;

    try
    {
        using (ESRI.ArcGIS.ADF.ComReleaser ComReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
        {
            //Create the field collection for editing
            IFields Fields = new FieldsClass();
            ComReleaser.ManageLifetime(Fields);
            IFieldsEdit FieldsEdit = (IFieldsEdit)Fields;

            //Create the geometry field
            IGeometryDef GeomDef = new GeometryDef();
            IGeometryDefEdit GeomDefEdit = (IGeometryDefEdit)GeomDef;
            GeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

            IField GeomField = new FieldClass();
            IFieldEdit GeomFieldEdit = (IFieldEdit)GeomField;
            GeomFieldEdit.Name_2 = ShapeFieldName;
            GeomFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            GeomFieldEdit.GeometryDef_2 = GeomDef;
            GeomDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();;

            FieldsEdit.AddField(GeomField);

            //Create the OID filed
            IField OidField = new FieldClass();
            IFieldEdit OidFieldEdit = (IFieldEdit)OidField;
            OidFieldEdit.Name_2 = "OID";
            OidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
            FieldsEdit.AddField(OidField);

            //Add a field for each column
            foreach (DataColumn col in Data.Columns)
            {
                IField2 field = new FieldClass();
                IFieldEdit fieldedit = (IFieldEdit)field;
                fieldedit.Name_2 = col.ColumnName;
                bool AddField = false;

                if (col.DataType.IsIn(typeof(Int64), typeof(Int32)))
                {
                    //Best we can do is 32 bit in a shapefile
                    fieldedit.Type_2 = esriFieldType.esriFieldTypeInteger;
                    AddField = true;
                }
                else if (col.DataType == typeof(Int16))
                {
                    fieldedit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
                    AddField = true;
                }
                else if (col.DataType == typeof(Single))
                {
                    fieldedit.Type_2 = esriFieldType.esriFieldTypeSingle;
                    AddField = true;
                }
                else if (col.DataType == typeof(double))
                {
                    fieldedit.Type_2 = esriFieldType.esriFieldTypeDouble;
                    AddField = true;
                }
                else if (col.DataType == typeof(string))
                {
                    fieldedit.Type_2 = esriFieldType.esriFieldTypeString;
                    //Here is where I get "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." 
                    //The higher the number of fields the fewer "runs" of this method needed to hit the error.
                    //At 150 it is hit right away but if it is at, say, 50 it can run many times.
                    if (Fields.FieldCount < 150) 
                        AddField = true;
                    else
                        System.Diagnostics.Debug.Write(field.Name + "; ");
                }
                else if (col.DataType == typeof(DateTime))
                {
                    fieldedit.Type_2 = esriFieldType.esriFieldTypeDate;
                    AddField = true;
                }
                else if (col.DataType == typeof(bool))
                {
                    fieldedit.Type_2 = esriFieldType.esriFieldTypeString;
                    AddField = true;
                }

                if (AddField)
                    FieldsEdit.AddField(field);

            }

            //FilePath is the ArcMap temp folder generated for the session so something like "C:\Users\USERNAME\AppData\Local\Temp\arc1097\\"
            IWorkspaceFactory WorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace FeatureWorkspace = (IFeatureWorkspace)WorkspaceFactory.OpenFromFile(FilePath, hWnd);
            ComReleaser.ManageLifetime(WorkspaceFactory);

            //FileName is a class memeber which might be something like "o2zkdrnv.shp" which is randomly generated
            FeatureClass = FeatureWorkspace.CreateFeatureClass(FileName, Fields, null, null, esriFeatureType.esriFTSimple, ShapeFieldName, "");  //<-- THE ERROR OCCURS HERE
            ComReleaser.ManageLifetime(FeatureClass);
        }
    }
}

所以我只能假设这是某种内存错误。我允许的列越多(带有“< 150”的行),在遇到错误之前我可以重复运行的次数越多。似乎 65 左右是我立即开始看到它的阈值。在此之下,我可以在击中它之前运行它,比如说,5-6 次,但这似乎非常不一致。如果我停止调试 VS 并重新启动项目,它似乎会更快发生。我添加了 ComReleaser 认为它可能会有所帮助(甚至认为它在技术上不是 ArcMap 命名空间的一部分),但它没有任何区别。

正在传递的数据表包含以下字段列表:

RecordID,SiteID,SiteName,SiteType,LocID,LocName,LocType,LocX,LocY,LocZ,LocLong,LocLat,LocTop,LocBot,LocGroup,SampID,SampName,SampDate,SampDate2,SampTime,SampTime2,SampType,SampGroup,SampMatrix,SampTop,cSampTop,SampBot,cSampBot,SampElev,cSampElev,SampUnit,AnalyteID,Analyte,Result,cResult,Flag,Unit,DetectLim,cDetectLim,IsDetect,IsTIC,IsReject,Method,Category,LabID,Comment,Source,RegName01,RegValue01,RegAbove01,RegName02,RegValue02,RegAbove02,RegName03,RegValue03,RegAbove03,RegName04,RegValue04,RegAbove04,RegName05,RegValue05,RegAbove05,RegName06,RegValue06,RegAbove06,RegName07,RegValue07,RegAbove07,RegName08,RegValue08,RegAbove08,RegName09,RegValue09,RegAbove09,RegName10,RegValue10,RegAbove10,RegName11,RegValue11,RegAbove11,RegName12,RegValue12,RegAbove12,RegName13,RegValue13,RegAbove13,RegName14,RegValue14,RegAbove14,RegName15,RegValue15,RegAbove15,RegName16,RegValue16,RegAbove16,RegName17,RegValue17,RegAbove17,RegName18,RegValue18,RegAbove18,RegName19,RegValue19,RegAbove19,RegName20,RegValue20,RegAbove20,RegName21,RegValue21,RegAbove21,RegName22,RegValue22,RegAbove22,RegName23,RegValue23,RegAbove23,RegName24,RegValue24,RegAbove24,RegName25,RegValue25,RegAbove25,RegName26,RegValue26,RegAbove26,RegName27,RegValue27,RegAbove27,RegName28,RegValue28,RegAbove28,RegName29,RegValue29,RegAbove29,RegName30,RegValue30,RegAbove30

从理论上讲,表格的内容应该无关紧要,因为此方法从中提取的唯一内容是 Columns 集合。

任何帮助都感激不尽。

4

0 回答 0