ThinkGeo.com    |     Blog    |     Wiki    |     Support

FileGeoDatabase -- Cannot access a disposed object

MapSuite Team,

When existing my application while in Visual Studio debugger the below error is encountered more times than not.

This does not happen when the application exe is run directly.

I’m running with MapSuite V10.5.x.

Any ideas?

Thanks,
Dennis

System.ObjectDisposedException
HResult=0x80131622
Message=Cannot access a disposed object.
Object name: ‘Geodatabase’.
Source=Esri.FileGDBAPI
StackTrace:
at Esri.FileGDB.Geodatabase.get_NativeGeodatabase()
at Esri.FileGDB.Table.Shutdown()
at Esri.FileGDB.Table.Dispose(Boolean A_0)
at Esri.FileGDB.Table.Finalize()

Hi Dennis,

It looks that’s thrown by the 3rd part dll, we don’t have their code, but from the exception information it should because the Geodatabase had been disposed somewhere.

Could you please build a simple sample which can reproduce it? So our developer can look into this, we hadn’t met this exception before.

Regards,

Ethan

Hi Dennis,

I reproduced this exception when I tried to close the application form.

If you also met that when you try to close application, I think that’s because your code hadn’t call close for this layer.

Generally you can close it after use it like this:

    map.CurrentExtent = fileGeoDatabaseFeatureLayer.GetBoundingBox();
    fileGeoDatabaseFeatureLayer.Close();
    map.Refresh();

Regards,

Ethan

hi Ethan,

I have implemented your suggestion to Close FeatureLayer s when the application is shutting down.

Application is running MapSuite V10.5.

Under Windows-10 it works in both debug mode and when running directly from the applications’ executable.

However, my target machines are Windows-7 and when running directly from the applications’ executable MapSuite encounters an Unhandled Exception upon FeatureLayer.Close. There are actually three different scenarios under which this occurs.

First…upon application startup nine GDB layers are instantiated. Each one in turn is Opened and then Closed simply to make sure the layers can be opened/closed. Under Windows-10 this works fine. However, under Windows-7 only the first layer may be opened and then closed. The second layer may be opened, but encounter the Unhandled Exception when attempting to Close. The following is the exception:

Log Name:      Application
Source:        .NET Runtime
Date:          8/23/2019 7:02:43 PM
Event ID:      1026
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      name
Description:
Application: application.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
   at <Module>.FileGDBAPI.Geodatabase.{ctor}(FileGDBAPI.Geodatabase*)
   at Esri.FileGDB.Geodatabase..ctor()
   at Esri.FileGDB.Geodatabase.Open(System.String)
   at FileGdbApiWrapper.FileGdbApiWrapper.GetGeodatabase(System.String)
   at FileGdbApiWrapper.FileGdbApiWrapper.GetTableNames(System.String)

Exception Info: System.Reflection.TargetInvocationException
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
   at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at ThinkGeo.MapSuite.Layers.FileGeoDatabaseFeatureSource.GetTableNames(System.String)
   at ThinkGeo.MapSuite.Layers.FileGeoDatabaseFeatureLayer.GetTableNames(System.String)
   at OriStarVisionMapSuite.clsMapLayerItemCollection.PrepareLayerFromDatabaseGdb(OriStarVisionMapSuite.clsMapLayerItem ByRef)
   at OriStarVisionMapSuite.clsMapLayerItemCollection.PrepareLayerFromDatabase(OriStarVisionMapSuite.clsMapLayerItem ByRef)
   at OriStarVisionMapSuite.clsMapLayerItemCollection.Load(OriStarTools.OleDb ByRef)

Second…If only one GDB layer is instantiated and only Opened at application start the following exception occurs when the application is shutdown and an attempt to Close the single GDB layer:

Log Name:      Application
Source:        .NET Runtime
Date:          8/23/2019 7:34:32 PM
Event ID:      1026
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      name
Description:
Application: application.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
   at <Module>.FileGDBAPI.Geodatabase.CloseTable(FileGDBAPI.Geodatabase*, FileGDBAPI.Table*)
   at Esri.FileGDB.Table.Shutdown()
   at Esri.FileGDB.Table.Dispose(Boolean)
   at Esri.FileGDB.Table.Finalize()

Third…All nine GDB layers are instantiated and just opened at application startup. All nine layers are rendered properly. When the application is shutdown and an attempt to Close GDB layers the following occurs:

Log Name:      Application
Source:        .NET Runtime
Date:          8/23/2019 7:59:15 PM
Event ID:      1026
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      name
Description:
Application: application.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
   at <Module>.FileGDBAPI.Geodatabase.CloseTable(FileGDBAPI.Geodatabase*, FileGDBAPI.Table*)
   at Esri.FileGDB.Table.Close()
   at FileGdbApiWrapper.FileGdbApiWrapper.DisposeGeodatatable(System.String, System.String)
   at FileGdbApiWrapper.FileGdbApiWrapper.Close()

Exception Info: System.Reflection.TargetInvocationException
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
   at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at ThinkGeo.MapSuite.Layers.FileGeoDatabaseFeatureSource.CloseCore()
   at ThinkGeo.MapSuite.Layers.FeatureSource.Close()
   at ThinkGeo.MapSuite.Layers.FeatureLayer.CloseCore()
   at ThinkGeo.MapSuite.Layers.Layer.Close()
   at OriStarVisionMapSuite.MapFunctions.FeatureLayerClose()
   at OriStarVisionMapSuite.ucMap.CloseMap()
   at ViewOfChicago.MainWindow.Window_Closing(System.Object, System.ComponentModel.CancelEventArgs)
   at System.Windows.Window.OnClosing(System.ComponentModel.CancelEventArgs)
   at System.Windows.Window.WmClose()
   at System.Windows.Window.WindowFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)

Regards,
Dennis

Hi Dennis,

I need modify the sample to reproduce that first then try to find why it get the different result between win7 and win10.

Any update I will let you know.

Regards,

Ethan

Hi Dennis,

It looks that’s related with the way to call system API, we hadn’t found solution for it, but you can try this workaround.

  1. If you use many FileGeoDatabaseFeatureLayers, make sure their data is different.

  2. You can open each one of them, but only need to close the first one like this:

    FileGeoDatabaseFeatureLayer fileGeoDatabaseFeatureLayer = new FileGeoDatabaseFeatureLayer(ThePath, TheTable);
    fileGeoDatabaseFeatureLayer.Open();

FileGeoDatabaseFeatureLayer fileGeoDatabaseFeatureLayer2 = new FileGeoDatabaseFeatureLayer(ThePath1, TheTable1);
fileGeoDatabaseFeatureLayer2.Open();

fileGeoDatabaseFeatureLayer.FeatureSource.Close();

It should works in win7 x64 and won’t throw exception.

Regards,

Ethan