Standalone program crashes after using Geoprocessor

Discussion created by chrislesh56 on Oct 25, 2012
Latest reply on Apr 25, 2013 by mkoneya
I'm writing a standalone application that executes the "Create feature class" tool using Geoprocessor.  The tool runs successfully.  However when the program exits is when I have a problem.  The program either hangs indefinitely or crashes upon exiting the Main method.  This is in C# using ArcGIS 10.1 against a geodatabase on SQL Server 2012.

I've read about the necessity of manually freeing COM objects sometimes in order to avoid these problems.  But all the sample code I've ever seen that uses Geoprocessor doesn't do this.  Various samples show the Geoprocessor object and whatever tool object is being used with it created normally using "new", with no special handling done to free them afterwards.

As a test I also tried running the "Copy" tool and had the same problem.  As another test I ran the same code against a file geodatabase and did not run into this issue.  It's against a geodatabase in SQL server that it happens.  I tried both an enterprise geodatabase and a SQL Express geodatabase with the same result.

I've pared my code down to the small example below that demonstrates the problem.  Again the tool runs successfully and the new feature class is created from the specified template.  No problem.  It's upon exit from Main that I get a crash.

Any ideas on how to just use Geoprocessor from C# successfully?

using System;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataManagementTools;

namespace TestGP
    class Program
        private static LicenseInitializer m_AOLicenseInitializer = new TestGP.LicenseInitializer();
        static void Main(string[] args)
                new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced },
                new esriLicenseExtensionCode[] { });

            string workspacePathname = @"C:\Users\username\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\connection.sde";

            Geoprocessor gp = new Geoprocessor();
            gp.SetEnvironmentValue("workspace", workspacePathname);

            CreateFeatureclass tool = new CreateFeatureclass();
            tool.out_path = workspacePathname;
            tool.out_name = "MyNewRoads";
            tool.template = "Geo1.DBO.RoadTemplate";
            tool.has_m = "SAME_AS_TEMPLATE";
            tool.has_z = "SAME_AS_TEMPLATE";

            object sev = null;
                gp.Execute(tool, null);
            catch (Exception exp)
                Console.WriteLine("Exception:  " + exp.Message);
                Console.WriteLine(gp.GetMessages(ref sev));
            Console.WriteLine("Created new feature class");