From 71b5410cf0afdf60cfe2dc286c884f88ea28e831 Mon Sep 17 00:00:00 2001 From: Lau Date: Mon, 23 Mar 2020 20:31:49 +0100 Subject: [PATCH] Fix dispose pattern for releasing the RCW object. Use `ReleaseComObject()` instead of `FinalReleaseComObject` to avoid creating stale references to the RCW of a potential singleton COM object. --- src/System.Data.Jet/ComObject.cs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/System.Data.Jet/ComObject.cs b/src/System.Data.Jet/ComObject.cs index 6be3e97..4ab5e91 100644 --- a/src/System.Data.Jet/ComObject.cs +++ b/src/System.Data.Jet/ComObject.cs @@ -10,15 +10,12 @@ namespace System.Data.Jet { private object _instance; #if DEBUG - private readonly Guid _trackingId; + private readonly Guid _trackingId = Guid.NewGuid(); #endif public ComObject(object instance) { _instance = instance; -#if DEBUG - _trackingId = Guid.NewGuid(); -#endif } public ComObject(string progid) @@ -111,24 +108,17 @@ namespace System.Data.Jet return obj; } - private void ReleaseUnmanagedResources() + public void Dispose() { + // The RCW is a .NET object and cannot be released from the finalizer anymore, + // because it might not exist anymore. if (_instance != null) { - Marshal.FinalReleaseComObject(_instance); + Marshal.ReleaseComObject(_instance); _instance = null; } - } - public void Dispose() - { - ReleaseUnmanagedResources(); GC.SuppressFinalize(this); } - - ~ComObject() - { - ReleaseUnmanagedResources(); - } } } \ No newline at end of file