Misc fix for issue that Juan hit (#4053)
authorMike McLaughlin <mikem@microsoft.com>
Mon, 10 Jul 2023 21:08:09 +0000 (14:08 -0700)
committerGitHub <noreply@github.com>
Mon, 10 Jul 2023 21:08:09 +0000 (14:08 -0700)
src/Microsoft.Diagnostics.DebugServices.Implementation/Runtime.cs
src/Microsoft.Diagnostics.DebugServices/ServiceContainer.cs

index 771e0d14c2a83bf5db361b40980e27d7a72da882..04325022be98ba59dbf6cd3d163b3c61a7639dbe 100644 (file)
@@ -22,6 +22,7 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation
         private readonly ClrInfo _clrInfo;
         private readonly ISymbolService _symbolService;
         private Version _runtimeVersion;
+        private ClrRuntime _clrRuntime;
         private string _dacFilePath;
         private string _dbiFilePath;
 
@@ -56,12 +57,10 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation
 
         void IDisposable.Dispose()
         {
-            if (_serviceContainer.TryGetCachedService(typeof(ClrRuntime), out object service))
-            {
-                // The DataTarget created in the RuntimeProvider is disposed here. The ClrRuntime
-                // instance is disposed below in DisposeServices().
-                ((ClrRuntime)service).DataTarget.Dispose();
-            }
+            // The DataTarget created in the RuntimeProvider is disposed here. The ClrRuntime
+            // instance is disposed below in DisposeServices().
+            _clrRuntime?.DataTarget.Dispose();
+            _clrRuntime = null;
             _serviceContainer.RemoveService(typeof(IRuntime));
             _serviceContainer.DisposeServices();
         }
@@ -125,7 +124,7 @@ namespace Microsoft.Diagnostics.DebugServices.Implementation
                 {
                     // Ignore the DAC version mismatch that can happen because the clrmd ELF dump reader
                     // returns 0.0.0.0 for the runtime module that the DAC is matched against.
-                    return _clrInfo.CreateRuntime(dacFilePath, ignoreMismatch: true);
+                    return _clrRuntime = _clrInfo.CreateRuntime(dacFilePath, ignoreMismatch: true);
                 }
                 catch (Exception ex) when
                    (ex is DllNotFoundException or
index d51b4d8cab813234382e84ae9d6cd4406f3a8d40..9b497c53fea676c7b3836874762552d76670e146 100644 (file)
@@ -73,30 +73,18 @@ namespace Microsoft.Diagnostics.DebugServices
         }
 
         /// <summary>
-        /// Get the cached/instantiated service instance if one exists. Don't call the factory or parent to create.
+        /// Returns the instance of the service or returns null if service doesn't exist
         /// </summary>
         /// <param name="type">service type</param>
-        /// <param name="service">service instance (can be null)</param>
-        /// <returns>if true, found service</returns>
-        public bool TryGetCachedService(Type type, out object service)
+        /// <returns>service instance or null</returns>
+        public object GetService(Type type)
         {
             Debug.Assert(type != null);
             if (type == typeof(IServiceProvider))
             {
-                service = this;
-                return true;
+                return this;
             }
-            return _instances.TryGetValue(type, out service);
-        }
-
-        /// <summary>
-        /// Returns the instance of the service or returns null if service doesn't exist
-        /// </summary>
-        /// <param name="type">service type</param>
-        /// <returns>service instance or null</returns>
-        public object GetService(Type type)
-        {
-            if (TryGetCachedService(type, out object service))
+            if (_instances.TryGetValue(type, out object service))
             {
                 return service;
             }