user1829700 user1829700 - 2 months ago 43
C# Question

ObjectDisposedException - Cannot access a disposed object

I am having some issues with my WPF Windows C# application. Whenever the application is closed, it throws a

. The problem seems to only occur when it is compiled in 32-bit. The 64-bit build runs fine without throwing any exception.

From the exception message, it says cannot access a disposed object but it doesn't seem to tell what object is causing the issue - How do I find out the object that has been disposed?

Exception:Thrown: "Cannot access a disposed object." (System.ObjectDisposedException)
A System.ObjectDisposedException was thrown: "Cannot access a disposed object."
Time: 1/19/2016 5:16:28 PM

Exception Info: System.ObjectDisposedException
at System.Diagnostics.EventLogInternal.OpenForWrite(System.String)
at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32, UInt16, System.Diagnostics.EventLogEntryType, System.String[], Byte[], System.String)
at System.Diagnostics.EventLogInternal.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32, Int16, Byte[])
at System.Diagnostics.EventLog.WriteEntry(System.String, System.Diagnostics.EventLogEntryType)
at HP.HPTRIM.SDK.TrimApplicationBase.UnregisterStackTrace(System.Object, Int32)
at HP.HPTRIM.SDK.Database.internal_Dispose()
at HP.HPTRIM.SDK.Database.Finalize()


Possibly this is too late to be useful but I stumbled on this because I've recently encountered a very similar (identical?) issue when using the HP Records Manager SDK (v8.3 in my case). I believe I've identified the cause.

There was a bug in my own code (an executable using the HP .Net SDK to connect to HPRM) where I hadn't called Dispose() on one of its SDK objects).

It seems that when the GC comes along to clean up as the executable exits, HP decides it wants to write a message to the Windows Application Event Log, pointing out that a developer forgot to .Dispose() of the object properly. For whatever reason, though, there seems to also be a bug in the HP SDK whereby, at this time, it'd disposed of its internal system for writing to the Event Log. (Sometimes it works for me, sometimes not. Probably a race condition somewhere in the SDK.)

The consequence was that after the end of my code, the .Net Framework jumped up and screamed about the HP SDK's attempt to use a disposed object for writing to the event log.

The immediate fix for me was to fix my own code. Once I'd disposed of all the objects I was meant to (specifically a Database connection I'd created), the chain of events wasn't kicked off. Really, though, the HP code also seems to be buggy.