OSDN Git Service

Disposeメソッドが適切に呼ばれるように修正
authorKimura Youichi <kim.upsilon@bucyou.net>
Thu, 7 Feb 2013 21:35:29 +0000 (06:35 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 8 Feb 2013 08:08:00 +0000 (17:08 +0900)
OpenTween/Bing.cs
OpenTween/ImageCache.cs
OpenTween/OTPictureBox.cs

index 8f389e9..bff930f 100644 (file)
@@ -174,21 +174,23 @@ namespace OpenTween
                 "&To=" + Uri.EscapeDataString("'" + _to + "'") +
                 "&$format=Raw";
 
-            var client = new WebClient();
-            client.Credentials = new NetworkCredential(ApplicationSettings.AzureMarketplaceKey, ApplicationSettings.AzureMarketplaceKey);
-            client.Encoding = Encoding.UTF8;
-
-            try
+            using (var client = new OTWebClient())
             {
-                var content = client.DownloadString(apiurl);
+                client.Credentials = new NetworkCredential(ApplicationSettings.AzureMarketplaceKey, ApplicationSettings.AzureMarketplaceKey);
+                client.Encoding = Encoding.UTF8;
 
-                buf = Regex.Replace(content, @"^<string[^>]*>(.*)</string>$", "$1");
-                return true;
-            }
-            catch (WebException)
-            {
-                buf = null;
-                return false;
+                try
+                {
+                    var content = client.DownloadString(apiurl);
+
+                    buf = Regex.Replace(content, @"^<string[^>]*>(.*)</string>$", "$1");
+                    return true;
+                }
+                catch (WebException)
+                {
+                    buf = null;
+                    return false;
+                }
             }
         }
 
index bbdddb0..5c449d7 100644 (file)
@@ -165,20 +165,34 @@ namespace OpenTween
             }
         }
 
-        public void Dispose()
+        protected virtual void Dispose(bool disposing)
         {
             if (this.disposed) return;
 
-            this.CancelAsync();
-
-            lock (this.lockObject)
+            if (disposing)
             {
-                foreach (var item in this.innerDictionary)
-                    item.Value.Dispose();
+                this.CancelAsync();
 
-                this.innerDictionary.Clear();
-                this.cancelTokenSource.Dispose();
+                lock (this.lockObject)
+                {
+                    foreach (var item in this.innerDictionary)
+                        item.Value.Dispose();
+
+                    this.innerDictionary.Clear();
+                    this.cancelTokenSource.Dispose();
+                }
             }
         }
+
+        public void Dispose()
+        {
+            this.Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        ~ImageCache()
+        {
+            this.Dispose(false);
+        }
     }
 }
index d366023..fb7d062 100644 (file)
@@ -111,6 +111,8 @@ namespace OpenTween
             var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
 
             return loadImageTask.ContinueWith(t => {
+                client.Dispose();
+
                 if (t.IsFaulted) throw t.Exception;
 
                 return MemoryImage.CopyFromBytes(t.Result);
@@ -264,6 +266,9 @@ namespace OpenTween
 
             if (this.memoryImage != null)
                 this.memoryImage.Dispose();
+
+            if (this.loadAsyncCancelTokenSource != null)
+                this.loadAsyncCancelTokenSource.Dispose();
         }
     }
 }