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.
pull/41/head
Lau 6 years ago
parent b117d4ea4a
commit 71b5410cf0

@ -10,15 +10,12 @@ namespace System.Data.Jet
{ {
private object _instance; private object _instance;
#if DEBUG #if DEBUG
private readonly Guid _trackingId; private readonly Guid _trackingId = Guid.NewGuid();
#endif #endif
public ComObject(object instance) public ComObject(object instance)
{ {
_instance = instance; _instance = instance;
#if DEBUG
_trackingId = Guid.NewGuid();
#endif
} }
public ComObject(string progid) public ComObject(string progid)
@ -111,24 +108,17 @@ namespace System.Data.Jet
return obj; 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) if (_instance != null)
{ {
Marshal.FinalReleaseComObject(_instance); Marshal.ReleaseComObject(_instance);
_instance = null; _instance = null;
} }
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
~ComObject()
{
ReleaseUnmanagedResources();
}
} }
} }
Loading…
Cancel
Save