Skip to content

Commit 825f5d7

Browse files
committed
DevToolsClient - AccessViolationException: Attempted to read or write protected memory
Issue #3725
1 parent 39bd689 commit 825f5d7

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

CefSharp/DevTools/DevToolsClient.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public partial class DevToolsClient : IDevToolsMessageObserver, IDevToolsClient
3030
private IRegistration devToolsRegistration;
3131
private bool devToolsAttached;
3232
private SynchronizationContext syncContext;
33+
private int disposeCount;
3334

3435
/// <inheritdoc/>
3536
public event EventHandler<DevToolsEventArgs> DevToolsEvent;
@@ -154,10 +155,18 @@ public async Task<DevToolsMethodResponse> ExecuteDevToolsMethodAsync(string meth
154155
/// <inheritdoc/>
155156
void IDisposable.Dispose()
156157
{
157-
DevToolsEvent = null;
158-
devToolsRegistration?.Dispose();
159-
devToolsRegistration = null;
160-
browser = null;
158+
//Dispose can be called from different Threads
159+
//CEF maintains a reference and the user
160+
//maintains a reference, we in a rare case
161+
//we end up disposing of #3725 twice from different
162+
//threads. This will ensure our dispose only runs once.
163+
if (Interlocked.Increment(ref disposeCount) == 1)
164+
{
165+
DevToolsEvent = null;
166+
devToolsRegistration?.Dispose();
167+
devToolsRegistration = null;
168+
browser = null;
169+
}
161170
}
162171

163172
/// <inheritdoc/>

0 commit comments

Comments
 (0)