Skip to content

Commit 71b5410

Browse files
committed
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.
1 parent b117d4e commit 71b5410

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

src/System.Data.Jet/ComObject.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ internal class ComObject : DynamicObject, IDisposable
1010
{
1111
private object _instance;
1212
#if DEBUG
13-
private readonly Guid _trackingId;
13+
private readonly Guid _trackingId = Guid.NewGuid();
1414
#endif
1515

1616
public ComObject(object instance)
1717
{
1818
_instance = instance;
19-
#if DEBUG
20-
_trackingId = Guid.NewGuid();
21-
#endif
2219
}
2320

2421
public ComObject(string progid)
@@ -111,24 +108,17 @@ private static object WrapIfRequired(object obj)
111108
return obj;
112109
}
113110

114-
private void ReleaseUnmanagedResources()
111+
public void Dispose()
115112
{
113+
// The RCW is a .NET object and cannot be released from the finalizer anymore,
114+
// because it might not exist anymore.
116115
if (_instance != null)
117116
{
118-
Marshal.FinalReleaseComObject(_instance);
117+
Marshal.ReleaseComObject(_instance);
119118
_instance = null;
120119
}
121-
}
122120

123-
public void Dispose()
124-
{
125-
ReleaseUnmanagedResources();
126121
GC.SuppressFinalize(this);
127122
}
128-
129-
~ComObject()
130-
{
131-
ReleaseUnmanagedResources();
132-
}
133123
}
134124
}

0 commit comments

Comments
 (0)