diff --git a/src/EditorFeatures/TestUtilities/Remote/InProcRemostHostClient.cs b/src/EditorFeatures/TestUtilities/Remote/InProcRemostHostClient.cs index 93ea0ebf829b0ef3e234b34b7590ae482192280f..d53c06d4833f0dd267266eaa7abbee568f1e2d64 100644 --- a/src/EditorFeatures/TestUtilities/Remote/InProcRemostHostClient.cs +++ b/src/EditorFeatures/TestUtilities/Remote/InProcRemostHostClient.cs @@ -96,7 +96,7 @@ public Task RequestServiceAsync(string serviceName) // this is what consumer actually use to communicate information var serviceStream = await _inprocServices.RequestServiceAsync(serviceName).ConfigureAwait(false); - return new JsonRpcConnection(_inprocServices.Logger, callbackTarget, serviceStream, _remotableDataRpc.TryAddReference()); + return new JsonRpcConnection(_inprocServices.Logger, callbackTarget, serviceStream, _remotableDataRpc.TryAddReference() ?? throw new ObjectDisposedException(GetType().FullName)); } protected override void OnStarted() diff --git a/src/Workspaces/Core/Portable/Utilities/IReferenceCountedDisposable.cs b/src/Workspaces/Core/Portable/Utilities/IReferenceCountedDisposable.cs index f477bf34693c4abf6e29c5667c27e02da31754a7..60b2b88ee3361bf580d6112ec0eeab4737622175 100644 --- a/src/Workspaces/Core/Portable/Utilities/IReferenceCountedDisposable.cs +++ b/src/Workspaces/Core/Portable/Utilities/IReferenceCountedDisposable.cs @@ -1,7 +1,8 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; -using System.Runtime.CompilerServices; namespace Roslyn.Utilities { @@ -38,6 +39,6 @@ internal interface IReferenceCountedDisposable : IDisposable /// A new pointing to the same underlying object, if it /// has not yet been disposed; otherwise, if this reference to the underlying object /// has already been disposed. - IReferenceCountedDisposable TryAddReference(); + IReferenceCountedDisposable? TryAddReference(); } } diff --git a/src/Workspaces/Core/Portable/Utilities/ReferenceCountedDisposable.cs b/src/Workspaces/Core/Portable/Utilities/ReferenceCountedDisposable.cs index 5d697fab3349e8112d410a6e70001a8bd0e09055..1c61486a377d4eceb8797ae9cfc09ad10e74cb52 100644 --- a/src/Workspaces/Core/Portable/Utilities/ReferenceCountedDisposable.cs +++ b/src/Workspaces/Core/Portable/Utilities/ReferenceCountedDisposable.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Runtime.CompilerServices; @@ -60,7 +62,7 @@ internal sealed class ReferenceCountedDisposable : IReferenceCountedDisposabl /// This value is only cleared in order to support cases where one or more references is garbage /// collected without having called. /// - private T _instance; + private T? _instance; /// /// The boxed reference count, which is shared by all references with the same object. @@ -123,12 +125,12 @@ private ReferenceCountedDisposable(T instance, StrongBox referenceCount) /// A new pointing to the same underlying object, if it /// has not yet been disposed; otherwise, if this reference to the underlying object /// has already been disposed. - public ReferenceCountedDisposable TryAddReference() + public ReferenceCountedDisposable? TryAddReference() { return TryAddReferenceImpl(_instance, _boxedReferenceCount); } - IReferenceCountedDisposable IReferenceCountedDisposable.TryAddReference() + IReferenceCountedDisposable? IReferenceCountedDisposable.TryAddReference() { return TryAddReference(); } @@ -137,7 +139,7 @@ IReferenceCountedDisposable IReferenceCountedDisposable.TryAddReference() /// Provides the implementation for and /// . /// - private static ReferenceCountedDisposable TryAddReferenceImpl(T target, StrongBox referenceCount) + private static ReferenceCountedDisposable? TryAddReferenceImpl(T? target, StrongBox referenceCount) { lock (referenceCount) { @@ -176,7 +178,7 @@ private static ReferenceCountedDisposable TryAddReferenceImpl(T target, Stron /// public void Dispose() { - T instanceToDispose = null; + T? instanceToDispose = null; lock (_boxedReferenceCount) { if (_instance == null) @@ -207,8 +209,8 @@ public struct WeakReference /// /// DO NOT DISPOSE OF THE TARGET. /// - private readonly WeakReference _weakInstance; - private readonly StrongBox _boxedReferenceCount; + private readonly WeakReference? _weakInstance; + private readonly StrongBox? _boxedReferenceCount; public WeakReference(ReferenceCountedDisposable reference) : this() @@ -247,10 +249,10 @@ public WeakReference(ReferenceCountedDisposable reference) /// A new pointing to the same underlying object, /// if it has not yet been disposed; otherwise, if the underlying object has /// already been disposed. - public ReferenceCountedDisposable TryAddReference() + public ReferenceCountedDisposable? TryAddReference() { var weakInstance = _weakInstance; - if (weakInstance == null || !_weakInstance.TryGetTarget(out var target)) + if (weakInstance == null || !weakInstance.TryGetTarget(out var target)) { return null; }