IFeatureWorkspace.CreateFeatureClass() error (C#)

1695
3
03-10-2011 03:50 AM
LukeBadgerow
New Contributor
I'm trying to read a csv file straight into a set of new featurce classes within a new file geodatabase (or a shapefileworkspacefactory, for now) but I am getting the following stack trace error when it fails:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt. ESRI.ArcGIS.Geodatabase at ESRI.ArcGIS.Geodatabase.IFeatureDataset.CreateFeatureClass(String Name, IFields Fields, UID CLSID, UID EXTCLSID, esriFeatureType FeatureType, String ShapeFieldName, String ConfigKeyword) at WaterEditorExtension.Model.SurveyUtilities.StreamWriting(String pointtype, String outfile, String[] record, IWorkspace workspace) in C:\Documents and Settings\lbadgerow\My Documents\Visual Studio 2008\Projects\WaterEditorExtension\WaterEditorExtension\Model\SurveyUtilities.cs:line


I had initially believed it was related to the shared lock on the FGDB, but after testing it against the ShapefileWorkspaceFactory with the same results I'm now completely baffled.  I've been fighting this issue for way too long now and it's holding my project up.  I've also attempted to place a COMReleaser() on the workspace, but that just creates a situation where the reference persists but the object is garbage collected.  Relevant sections of code are below, any advice is greatly appreciated:

        public static IFeatureWorkspace CreateWorkspace(String path, String jobnumber)
        {
            //Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
            //IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
            //workspaceFactory.Create(path, jobnumber, null, 0);
            //IWorkspace workspace = workspaceFactory.OpenFromFile(path + jobnumber + ".gdb", 0);
            ShapefileWorkspaceFactory ws = new ShapefileWorkspaceFactory();
            String outpath = path + jobnumber;
            System.IO.Directory.CreateDirectory(outpath);
            IFeatureWorkspace workspace = (IFeatureWorkspace)ws.OpenFromFile(outpath,0);
            return workspace;
        }
...
...
            var CLSID = new UIDClass();
            CLSID.Value = "esriGeodatabase.Feature";
            IFields outfields = CreateFields();
 
            try
            {
                IFieldChecker fieldchecker = new FieldCheckerClass();
                IEnumFieldError enumfielderror = null;
                IFields validatedFields = null;
                fieldchecker.ValidateWorkspace = (IWorkspace)workspace;
                fieldchecker.Validate(outfields, out enumfielderror, out validatedFields);
                workspace.CreateFeatureClass("test.shp", outfields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
            }
            catch (Exception exc)
            {
                throw new Exception(String.Format("Error {0}", exc.Message));
            }
0 Kudos
3 Replies
LukeBadgerow
New Contributor
EDIT 17 March 2011:  has anyone attempted to Thread a component (extension) of ArcMap?  I'm thinking that I could release the schema lock that is preventing this from moving on to the FC creation phase of this project, but I can't seem to get ArcMap to remain stable when I create a new thread to build the gdb.
0 Kudos
JohnHauck
Occasional Contributor II
I'm not seeing this happen. Please let me know what you see with the following or if I'm missing something with this:

        protected override void OnClick()
        {
            IWorkspace workspace = CreateWorkspace("C:\\temp", "n_" + DateTime.Now.Millisecond.ToString());
            IFeatureClass fc = CreateFeatureClass("fc" + DateTime.Now.Millisecond.ToString(), workspace);

            ArcMap.Application.CurrentTool = null;
        }

        private IWorkspace CreateWorkspace(string parentFolder, string newFolderName)
        {
            ShapefileWorkspaceFactory ws = new ShapefileWorkspaceFactory();
            IWorkspaceName workspaceName = ws.Create(parentFolder, newFolderName, null, 0);
            IWorkspace workspace = (IWorkspace)((IName)workspaceName).Open();
            return workspace;
        }

        private IFeatureClass CreateFeatureClass(String name, IWorkspace workspace)
        {
            IFeatureWorkspace fw = (IFeatureWorkspace)workspace;

            IObjectClassDescription objectClassDescription = new FeatureClassDescriptionClass();
            IFields outfields = objectClassDescription.RequiredFields;

            UID CLSID = new UIDClass();
            CLSID.Value = "esriGeodatabase.Feature";

            IFeatureClass outfc = fw.CreateFeatureClass(name, outfields, CLSID, null, esriFeatureType.esriFTSimple, "SHAPE", "");

            return outfc;
        }



As far as the threading goes make sure you follow the Threads in Isolation model:

Writing multithreaded ArcObjects code
0 Kudos
LukeBadgerow
New Contributor
Thanks John.

We believe that we tracked my issue back to a bug within the MS side of the .Net framework (I don't have the KB number for it right off), and our desktop support guys were saying that we might not have been able to get a hold of the hotfix, so I ended up wrapping a quick python script into the logic.  It's not as quick, but I think in the end it will give us an access point that our editors/analysts will be able to use to tweak the process if they need to.

thanks again.
0 Kudos