未验证 提交 ee1cf15c 编写于 作者: T Tomáš Matoušek 提交者: GitHub

Remove Dispose(disposing) from objects without finalization (#40782)

上级 d25991bb
...@@ -343,14 +343,9 @@ public Task<bool> WriteStreamAsync(Document document, string name, Stream stream ...@@ -343,14 +343,9 @@ public Task<bool> WriteStreamAsync(Document document, string name, Stream stream
return SpecializedTasks.True; return SpecializedTasks.True;
} }
protected virtual void Dispose(bool disposing) public virtual void Dispose()
{ {
} }
public void Dispose()
{
Dispose(true);
}
} }
} }
} }
......
...@@ -103,14 +103,14 @@ protected override void OnStarted() ...@@ -103,14 +103,14 @@ protected override void OnStarted()
{ {
} }
protected override void Dispose(bool disposing) public override void Dispose()
{ {
// we are asked to disconnect. unsubscribe and dispose to disconnect // we are asked to disconnect. unsubscribe and dispose to disconnect
_endPoint.Disconnected -= OnDisconnected; _endPoint.Disconnected -= OnDisconnected;
_endPoint.Dispose(); _endPoint.Dispose();
_remotableDataRpc.Dispose(); _remotableDataRpc.Dispose();
base.Dispose(disposing); base.Dispose();
} }
private void OnDisconnected(JsonRpcDisconnectedEventArgs e) private void OnDisconnected(JsonRpcDisconnectedEventArgs e)
......
...@@ -236,26 +236,18 @@ private bool TryGetBuffer(uint docCookie, out ITextBuffer textBuffer) ...@@ -236,26 +236,18 @@ private bool TryGetBuffer(uint docCookie, out ITextBuffer textBuffer)
return false; return false;
} }
#region IDisposable Support public void Dispose()
private void Dispose(bool disposing)
{ {
if (!_isDisposed) if (_isDisposed)
{ {
if (disposing) return;
{
var runningDocumentTableForEvents = (IVsRunningDocumentTable)_runningDocumentTable;
runningDocumentTableForEvents.UnadviseRunningDocTableEvents(_runningDocumentTableEventsCookie);
_runningDocumentTableEventsCookie = 0;
}
_isDisposed = true;
} }
}
public void Dispose() var runningDocumentTableForEvents = (IVsRunningDocumentTable)_runningDocumentTable;
{ runningDocumentTableForEvents.UnadviseRunningDocTableEvents(_runningDocumentTableEventsCookie);
Dispose(true); _runningDocumentTableEventsCookie = 0;
_isDisposed = true;
} }
#endregion
} }
} }
...@@ -52,17 +52,14 @@ public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> ...@@ -52,17 +52,14 @@ public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object>
public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> directStreamReader, CancellationToken cancellationToken) public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> directStreamReader, CancellationToken cancellationToken)
=> _serviceEndPoint.InvokeAsync(targetName, arguments, directStreamReader, cancellationToken); => _serviceEndPoint.InvokeAsync(targetName, arguments, directStreamReader, cancellationToken);
protected override void Dispose(bool disposing) protected override void DisposeImpl()
{ {
if (disposing) // dispose service and snapshot channels
{ _serviceEndPoint.UnexpectedExceptionThrown -= UnexpectedExceptionThrown;
// dispose service and snapshot channels _serviceEndPoint.Dispose();
_serviceEndPoint.UnexpectedExceptionThrown -= UnexpectedExceptionThrown; _remoteDataRpc.Dispose();
_serviceEndPoint.Dispose();
_remoteDataRpc.Dispose();
}
base.Dispose(disposing); base.DisposeImpl();
} }
} }
} }
...@@ -35,14 +35,10 @@ public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> ...@@ -35,14 +35,10 @@ public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object>
public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> directStreamReader, CancellationToken cancellationToken) public override Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> directStreamReader, CancellationToken cancellationToken)
=> _connection.InvokeAsync(targetName, arguments, directStreamReader, cancellationToken); => _connection.InvokeAsync(targetName, arguments, directStreamReader, cancellationToken);
protected override void Dispose(bool disposing) protected override void DisposeImpl()
{ {
if (disposing) _connectionManager.Free(_serviceName, _connection);
{ base.DisposeImpl();
_connectionManager.Free(_serviceName, _connection);
}
base.Dispose(disposing);
} }
} }
} }
......
...@@ -169,7 +169,7 @@ protected override void OnStarted() ...@@ -169,7 +169,7 @@ protected override void OnStarted()
RegisterGlobalOperationNotifications(); RegisterGlobalOperationNotifications();
} }
protected override void Dispose(bool disposing) public override void Dispose()
{ {
// cancel all pending async work // cancel all pending async work
_shutdownCancellationTokenSource.Cancel(); _shutdownCancellationTokenSource.Cancel();
...@@ -187,7 +187,7 @@ protected override void Dispose(bool disposing) ...@@ -187,7 +187,7 @@ protected override void Dispose(bool disposing)
_connectionManager.Dispose(); _connectionManager.Dispose();
base.Dispose(disposing); base.Dispose();
} }
public HostGroup HostGroup public HostGroup HostGroup
......
...@@ -333,12 +333,10 @@ public MyUpdateSource(Workspace workspace) ...@@ -333,12 +333,10 @@ public MyUpdateSource(Workspace workspace)
public override Workspace Workspace => _workspace; public override Workspace Workspace => _workspace;
} }
private class InvokeThrowsCancellationConnection : RemoteHostClient.Connection private sealed class InvokeThrowsCancellationConnection : RemoteHostClient.Connection
{ {
private readonly CancellationTokenSource _source; private readonly CancellationTokenSource _source;
public bool Disposed = false;
public InvokeThrowsCancellationConnection(CancellationTokenSource source) public InvokeThrowsCancellationConnection(CancellationTokenSource source)
{ {
_source = source; _source = source;
...@@ -360,13 +358,6 @@ public override Task InvokeAsync(string targetName, IReadOnlyList<object> argume ...@@ -360,13 +358,6 @@ public override Task InvokeAsync(string targetName, IReadOnlyList<object> argume
public override Task<T> InvokeAsync<T>( public override Task<T> InvokeAsync<T>(
string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> funcWithDirectStreamAsync, CancellationToken cancellationToken) string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> funcWithDirectStreamAsync, CancellationToken cancellationToken)
=> throw new NotImplementedException(); => throw new NotImplementedException();
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Disposed = true;
}
} }
} }
} }
...@@ -77,12 +77,6 @@ public virtual Task DisposeAsync() ...@@ -77,12 +77,6 @@ public virtual Task DisposeAsync()
return Task.CompletedTask; return Task.CompletedTask;
} }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual MessageFilter RegisterMessageFilter() protected virtual MessageFilter RegisterMessageFilter()
=> new MessageFilter(); => new MessageFilter();
...@@ -93,17 +87,13 @@ protected void Wait(double seconds) ...@@ -93,17 +87,13 @@ protected void Wait(double seconds)
} }
/// <summary> /// <summary>
/// This method provides the implementation for <see cref="IDisposable.Dispose"/>. This method via the /// This method provides the implementation for <see cref="IDisposable.Dispose"/>.
/// <see cref="IDisposable"/> interface (i.e. <paramref name="disposing"/> is <see langword="true"/>) if the /// This method is called via the <see cref="IDisposable"/> interface if the constructor completes successfully.
/// constructor completes successfully. The <see cref="InitializeAsync"/> may or may not have completed /// The <see cref="InitializeAsync"/> may or may not have completed successfully.
/// successfully.
/// </summary> /// </summary>
protected virtual void Dispose(bool disposing) public virtual void Dispose()
{ {
if (disposing) _messageFilter.Dispose();
{
_messageFilter.Dispose();
}
} }
protected KeyPress Ctrl(VirtualKey virtualKey) protected KeyPress Ctrl(VirtualKey virtualKey)
......
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
namespace Microsoft.VisualStudio.IntegrationTest.Utilities.Harness namespace Microsoft.VisualStudio.IntegrationTest.Utilities.Harness
{ {
public class MessageFilter : IMessageFilter, IDisposable public sealed class MessageFilter : IMessageFilter, IDisposable
{ {
protected const uint CancelCall = ~0U; private const uint CancelCall = ~0U;
private readonly MessageFilterSafeHandle _messageFilterRegistration; private readonly MessageFilterSafeHandle _messageFilterRegistration;
private readonly TimeSpan _timeout; private readonly TimeSpan _timeout;
...@@ -28,12 +28,12 @@ public MessageFilter(TimeSpan timeout, TimeSpan retryDelay) ...@@ -28,12 +28,12 @@ public MessageFilter(TimeSpan timeout, TimeSpan retryDelay)
_messageFilterRegistration = MessageFilterSafeHandle.Register(this); _messageFilterRegistration = MessageFilterSafeHandle.Register(this);
} }
public virtual uint HandleInComingCall(uint dwCallType, IntPtr htaskCaller, uint dwTickCount, INTERFACEINFO[] lpInterfaceInfo) public uint HandleInComingCall(uint dwCallType, IntPtr htaskCaller, uint dwTickCount, INTERFACEINFO[] lpInterfaceInfo)
{ {
return (uint)SERVERCALL.SERVERCALL_ISHANDLED; return (uint)SERVERCALL.SERVERCALL_ISHANDLED;
} }
public virtual uint RetryRejectedCall(IntPtr htaskCallee, uint dwTickCount, uint dwRejectType) public uint RetryRejectedCall(IntPtr htaskCallee, uint dwTickCount, uint dwRejectType)
{ {
if ((SERVERCALL)dwRejectType != SERVERCALL.SERVERCALL_RETRYLATER if ((SERVERCALL)dwRejectType != SERVERCALL.SERVERCALL_RETRYLATER
&& (SERVERCALL)dwRejectType != SERVERCALL.SERVERCALL_REJECTED) && (SERVERCALL)dwRejectType != SERVERCALL.SERVERCALL_REJECTED)
...@@ -49,23 +49,14 @@ public virtual uint RetryRejectedCall(IntPtr htaskCallee, uint dwTickCount, uint ...@@ -49,23 +49,14 @@ public virtual uint RetryRejectedCall(IntPtr htaskCallee, uint dwTickCount, uint
return (uint)_retryDelay.TotalMilliseconds; return (uint)_retryDelay.TotalMilliseconds;
} }
public virtual uint MessagePending(IntPtr htaskCallee, uint dwTickCount, uint dwPendingType) public uint MessagePending(IntPtr htaskCallee, uint dwTickCount, uint dwPendingType)
{ {
return (uint)PENDINGMSG.PENDINGMSG_WAITDEFPROCESS; return (uint)PENDINGMSG.PENDINGMSG_WAITDEFPROCESS;
} }
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_messageFilterRegistration.Dispose();
}
}
public void Dispose() public void Dispose()
{ {
Dispose(true); _messageFilterRegistration.Dispose();
GC.SuppressFinalize(this);
} }
} }
} }
...@@ -53,12 +53,7 @@ protected void Started() ...@@ -53,12 +53,7 @@ protected void Started()
OnStatusChanged(started: true); OnStatusChanged(started: true);
} }
public void Dispose() public virtual void Dispose()
{
Dispose(disposing: true);
}
protected virtual void Dispose(bool disposing)
{ {
OnStatusChanged(started: false); OnStatusChanged(started: false);
} }
...@@ -220,7 +215,7 @@ protected Connection() ...@@ -220,7 +215,7 @@ protected Connection()
public abstract Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, CancellationToken cancellationToken); public abstract Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, CancellationToken cancellationToken);
public abstract Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> funcWithDirectStreamAsync, CancellationToken cancellationToken); public abstract Task<T> InvokeAsync<T>(string targetName, IReadOnlyList<object> arguments, Func<Stream, CancellationToken, Task<T>> funcWithDirectStreamAsync, CancellationToken cancellationToken);
protected virtual void Dispose(bool disposing) protected virtual void DisposeImpl()
{ {
// do nothing // do nothing
} }
...@@ -234,7 +229,7 @@ public void Dispose() ...@@ -234,7 +229,7 @@ public void Dispose()
_disposed = true; _disposed = true;
Dispose(disposing: true); DisposeImpl();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
......
...@@ -169,18 +169,9 @@ public Stream CreateWritableStream() ...@@ -169,18 +169,9 @@ public Stream CreateWritableStream()
public void Dispose() public void Dispose()
{ {
Dispose(true); // See remarks on field for relation between _memoryMappedFile and the views/streams. There is no
GC.SuppressFinalize(this); // need to write _weakReadAccessor here since lifetime of the target is not owned by this instance.
} _memoryMappedFile.Dispose();
private void Dispose(bool disposing)
{
if (disposing)
{
// See remarks on field for relation between _memoryMappedFile and the views/streams. There is no
// need to write _weakReadAccessor here since lifetime of the target is not owned by this instance.
_memoryMappedFile.Dispose();
}
} }
private unsafe sealed class SharedReadableStream : Stream, ISupportDirectMemoryAccess private unsafe sealed class SharedReadableStream : Stream, ISupportDirectMemoryAccess
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册