Layers from SDE Database are shown as invalid

599
2
Jump to solution
02-24-2021 03:47 PM
ScottDickison1
New Contributor

Here is the scenario. I open an MXD using IMapDocument.Open. This MXD contains layers that read from our 10.2.1 Enterprise Geodatabase. I do some operation and then close the MapDocument using IMapDocument.Close(). I open another MXD that has layers from the same 10.2.1 Enterprise Geodatabase immediately thereafter and do a similar process. This time the process fails because it can't get the FeatureClass from the FeatureLayer.  I have created a small console app to demonstrate the problem. I am currently using ArcObjects 10.6.1. If you put a breakpoint at the line  mapDocument2.Close() and check the value of FeatureClass on FL2 it returns as null.

 

What have I missed here?

 

using ESRI.ArcGIS.esriSystem;
using System;
using ESRI.ArcGIS.Carto;
using System.Runtime.InteropServices;

namespace TestApp
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new TestApp.LicenseInitializer();
        [System.Runtime.InteropServices.DllImport("user32")]
        public static extern int GetDesktopWindow();

        [STAThread()]
        static void Main(string[] args)
        {

            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced },
            new esriLicenseExtensionCode[] { });
            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()

            IMapDocument mapDocument = new MapDocumentClass();
            mapDocument.Open(@"\\pc.lojic.local\files\ArcGIS\10.6.1\Desktop\Development\Msd\Atlases\templates\forcemaincover.mxd");
            IActiveView pAV = (IActiveView)mapDocument.PageLayout;
            pAV.Activate(GetDesktopWindow());
            IFeatureLayer pFL = FindLayer(mapDocument.ActiveView.FocusMap, "ForceMains");
            Console.WriteLine(pFL.Name);

            pFL = null;
            pAV.Deactivate();
            pAV = null;
            mapDocument.Close();
            mapDocument = null;

            IMapDocument mapDocument2 = new MapDocumentClass();
            mapDocument2.Open(@"\\pc.lojic.local\files\ArcGIS\10.6.1\Desktop\Development\Msd\Atlases\templates\forcemains.mxd");
            IActiveView pAV2 = (IActiveView)mapDocument2.PageLayout;
            pAV2.Activate(GetDesktopWindow());
            IFeatureLayer pFL2 = FindLayer(mapDocument2.ActiveView.FocusMap, "ForceMains");
            mapDocument2.Close();
          
            m_AOLicenseInitializer.ShutdownApplication();
        }

       
        public static IFeatureLayer FindLayer(IMap pmap, string aLayerName)
        { 
            IEnumLayer pEnumLayer = pmap.Layers;
            ILayer pLayer;
            while ((pLayer = pEnumLayer.Next()) != null)
            {
                Console.WriteLine(pLayer.Name + " " + pLayer.Valid.ToString());
                if (pLayer.Name == aLayerName)
                    return (IFeatureLayer)pLayer;
            }
            return null;
        }
    }
}

 

 

0 Kudos
1 Solution

Accepted Solutions
KirkKuykendall1
Occasional Contributor III

Did you try running ReleaseCOMObject on all the non-null variables before calling ShutdownApplication?

Or something like this:

https://stackoverflow.com/a/38170605/125400

View solution in original post

0 Kudos
2 Replies
KirkKuykendall1
Occasional Contributor III

Did you try running ReleaseCOMObject on all the non-null variables before calling ShutdownApplication?

Or something like this:

https://stackoverflow.com/a/38170605/125400

0 Kudos
ScottDickison1
New Contributor

I now have it working. Thank you for pointing me in the right direction!

I had tried using Marshal.ReleaseCOMObject to no avail before. The Stack Overflow post you referenced mentioned explicitly calling Garbage Collection and then waiting for it to complete. Specifically these two lines: 

GC.Collect();
GC.WaitForPendingFinalizers();

 

I placed those between the mapDocument.Close() and the mapDocument.Open() and it worked - at least in my test app. I'm gong to swtich back over to the project i"m working and and see if it works there.  

 

Thanks again!!

0 Kudos