From 53a7ccb99e3300cb9b3600af0a943cee70ca239f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=B4=E5=A8=83=E9=85=B1?= Date: Wed, 26 Jul 2017 07:40:06 +0800 Subject: [PATCH] fix mem leaks --- shadowsocks-csharp/Controller/SystemProxy.cs | 2 +- shadowsocks-csharp/Util/Util.cs | 24 ++-- shadowsocks-csharp/View/LogForm.cs | 10 +- shadowsocks-csharp/View/MenuViewController.cs | 132 +++++++++--------- shadowsocks-csharp/View/SubscribeForm.cs | 14 ++ 5 files changed, 105 insertions(+), 77 deletions(-) diff --git a/shadowsocks-csharp/Controller/SystemProxy.cs b/shadowsocks-csharp/Controller/SystemProxy.cs index ffd55cff..0dfb7f13 100755 --- a/shadowsocks-csharp/Controller/SystemProxy.cs +++ b/shadowsocks-csharp/Controller/SystemProxy.cs @@ -240,7 +240,7 @@ private static void SetIEProxy(bool enable, bool global, string proxyServer, str foreach (INTERNET_PER_CONN_OPTION eachOption in _optionlist) { Marshal.StructureToPtr(eachOption, current, false); - current = (IntPtr)((int)current + Marshal.SizeOf(eachOption)); + current = (IntPtr)((long)current + Marshal.SizeOf(eachOption)); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index 780ce10b..734625aa 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -31,6 +31,8 @@ public static LRUCache DnsBuffer } } + static Process current_process = Process.GetCurrentProcess(); + public static void ReleaseMemory() { #if !_CONSOLE @@ -45,13 +47,13 @@ public static void ReleaseMemory() if (UIntPtr.Size == 4) { - SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, + SetProcessWorkingSetSize(current_process.Handle, (UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF); } else if (UIntPtr.Size == 8) { - SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, + SetProcessWorkingSetSize(current_process.Handle, (UIntPtr)0xFFFFFFFFFFFFFFFF, (UIntPtr)0xFFFFFFFFFFFFFFFF); } @@ -464,8 +466,10 @@ public static int RunAsAdmin(string Arguments) public static int GetDpiMul() { int dpi; - Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); - dpi = (int)graphics.DpiX; + using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) + { + dpi = (int)graphics.DpiX; + } return (dpi * 4 + 48) / 96; } @@ -478,12 +482,14 @@ public enum DeviceCap public static Point GetScreenPhysicalSize() { - Graphics g = Graphics.FromHwnd(IntPtr.Zero); - IntPtr desktop = g.GetHdc(); - int PhysicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPHORZRES); - int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); + using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) + { + IntPtr desktop = g.GetHdc(); + int PhysicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPHORZRES); + int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); - return new Point(PhysicalScreenWidth, PhysicalScreenHeight); + return new Point(PhysicalScreenWidth, PhysicalScreenHeight); + } } [DllImport("gdi32.dll")] diff --git a/shadowsocks-csharp/View/LogForm.cs b/shadowsocks-csharp/View/LogForm.cs index dd4d2a8b..8919bdc5 100644 --- a/shadowsocks-csharp/View/LogForm.cs +++ b/shadowsocks-csharp/View/LogForm.cs @@ -129,11 +129,13 @@ private void LogForm_Shown(object sender, EventArgs e) private void fontToolStripMenuItem_Click(object sender, EventArgs e) { - var fontDialog = new FontDialog(); - fontDialog.Font = logTextBox.Font; - if (fontDialog.ShowDialog() == DialogResult.OK) + using (FontDialog fontDialog = new FontDialog()) { - logTextBox.Font = fontDialog.Font; + fontDialog.Font = logTextBox.Font; + if (fontDialog.ShowDialog() == DialogResult.OK) + { + logTextBox.Font = fontDialog.Font; + } } } diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index e2533e40..fa7b7d88 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -135,13 +135,16 @@ private void UpdateTrayIcon() bool global = config.sysProxyMode == (int)ProxyMode.Global; bool random = config.random; - Bitmap icon = null; try { - icon = new Bitmap("icon.png"); + using (Bitmap icon = new Bitmap("icon.png")) + { + _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon()); + } } catch { + Bitmap icon = null; if (dpi < 97) { // dpi = 96; @@ -171,8 +174,8 @@ private void UpdateTrayIcon() mul_r = 0.4; } + using (Bitmap iconCopy = new Bitmap(icon)) { - Bitmap iconCopy = new Bitmap(icon); for (int x = 0; x < iconCopy.Width; x++) { for (int y = 0; y < iconCopy.Height; y++) @@ -185,10 +188,9 @@ private void UpdateTrayIcon() ((byte)(color.B * mul_b)))); } } - icon = iconCopy; + _notifyIcon.Icon = Icon.FromHandle(iconCopy.GetHicon()); } } - _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon()); // we want to show more details but notify icon title is limited to 63 characters string text = (enabled ? @@ -857,20 +859,22 @@ private void Config_Click(object sender, EventArgs e) private void Import_Click(object sender, EventArgs e) { - OpenFileDialog dlg = new OpenFileDialog(); - dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath; - if (dlg.ShowDialog() == DialogResult.OK) + using (OpenFileDialog dlg = new OpenFileDialog()) { - string name = dlg.FileName; - Configuration cfg = Configuration.LoadFile(name); - if (cfg.configs.Count == 1 && cfg.configs[0].server == Configuration.GetDefaultServer().server) - { - MessageBox.Show("Load config file failed", "ShadowsocksR"); - } - else + dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath; + if (dlg.ShowDialog() == DialogResult.OK) { - controller.MergeConfiguration(cfg); - LoadCurrentConfiguration(); + string name = dlg.FileName; + Configuration cfg = Configuration.LoadFile(name); + if (cfg.configs.Count == 1 && cfg.configs[0].server == Configuration.GetDefaultServer().server) + { + MessageBox.Show("Load config file failed", "ShadowsocksR"); + } + else + { + controller.MergeConfiguration(cfg); + LoadCurrentConfiguration(); + } } } } @@ -1169,32 +1173,33 @@ private void CopyAddress_Click(object sender, EventArgs e) private bool ScanQRCode(Screen screen, Bitmap fullImage, Rectangle cropRect, out string url, out Rectangle rect) { - Bitmap target = new Bitmap(cropRect.Width, cropRect.Height); - - using (Graphics g = Graphics.FromImage(target)) - { - g.DrawImage(fullImage, new Rectangle(0, 0, cropRect.Width, cropRect.Height), - cropRect, - GraphicsUnit.Pixel); - } - var source = new BitmapLuminanceSource(target); - var bitmap = new BinaryBitmap(new HybridBinarizer(source)); - QRCodeReader reader = new QRCodeReader(); - var result = reader.decode(bitmap); - if (result != null) + using (Bitmap target = new Bitmap(cropRect.Width, cropRect.Height)) { - url = result.Text; - double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0; - foreach (ResultPoint point in result.ResultPoints) + using (Graphics g = Graphics.FromImage(target)) + { + g.DrawImage(fullImage, new Rectangle(0, 0, cropRect.Width, cropRect.Height), + cropRect, + GraphicsUnit.Pixel); + } + var source = new BitmapLuminanceSource(target); + var bitmap = new BinaryBitmap(new HybridBinarizer(source)); + QRCodeReader reader = new QRCodeReader(); + var result = reader.decode(bitmap); + if (result != null) { - minX = Math.Min(minX, point.X); - minY = Math.Min(minY, point.Y); - maxX = Math.Max(maxX, point.X); - maxY = Math.Max(maxY, point.Y); + url = result.Text; + double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0; + foreach (ResultPoint point in result.ResultPoints) + { + minX = Math.Min(minX, point.X); + minY = Math.Min(minY, point.Y); + maxX = Math.Max(maxX, point.X); + maxY = Math.Max(maxY, point.Y); + } + //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY)); + rect = new Rectangle(cropRect.Left + (int)minX, cropRect.Top + (int)minY, (int)(maxX - minX), (int)(maxY - minY)); + return true; } - //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY)); - rect = new Rectangle(cropRect.Left + (int)minX, cropRect.Top + (int)minY, (int)(maxX - minX), (int)(maxY - minY)); - return true; } url = ""; rect = new Rectangle(); @@ -1203,32 +1208,33 @@ private bool ScanQRCode(Screen screen, Bitmap fullImage, Rectangle cropRect, out private bool ScanQRCodeStretch(Screen screen, Bitmap fullImage, Rectangle cropRect, double mul, out string url, out Rectangle rect) { - Bitmap target = new Bitmap((int)(cropRect.Width * mul), (int)(cropRect.Height * mul)); - - using (Graphics g = Graphics.FromImage(target)) + using (Bitmap target = new Bitmap((int)(cropRect.Width * mul), (int)(cropRect.Height * mul))) { - g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height), - cropRect, - GraphicsUnit.Pixel); - } - var source = new BitmapLuminanceSource(target); - var bitmap = new BinaryBitmap(new HybridBinarizer(source)); - QRCodeReader reader = new QRCodeReader(); - var result = reader.decode(bitmap); - if (result != null) - { - url = result.Text; - double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0; - foreach (ResultPoint point in result.ResultPoints) + using (Graphics g = Graphics.FromImage(target)) + { + g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height), + cropRect, + GraphicsUnit.Pixel); + } + var source = new BitmapLuminanceSource(target); + var bitmap = new BinaryBitmap(new HybridBinarizer(source)); + QRCodeReader reader = new QRCodeReader(); + var result = reader.decode(bitmap); + if (result != null) { - minX = Math.Min(minX, point.X); - minY = Math.Min(minY, point.Y); - maxX = Math.Max(maxX, point.X); - maxY = Math.Max(maxY, point.Y); + url = result.Text; + double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0; + foreach (ResultPoint point in result.ResultPoints) + { + minX = Math.Min(minX, point.X); + minY = Math.Min(minY, point.Y); + maxX = Math.Max(maxX, point.X); + maxY = Math.Max(maxY, point.Y); + } + //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY)); + rect = new Rectangle(cropRect.Left + (int)(minX / mul), cropRect.Top + (int)(minY / mul), (int)((maxX - minX) / mul), (int)((maxY - minY) / mul)); + return true; } - //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY)); - rect = new Rectangle(cropRect.Left + (int)(minX / mul), cropRect.Top + (int)(minY / mul), (int)((maxX - minX) / mul), (int)((maxY - minY) / mul)); - return true; } url = ""; rect = new Rectangle(); diff --git a/shadowsocks-csharp/View/SubscribeForm.cs b/shadowsocks-csharp/View/SubscribeForm.cs index 821b0918..f57dc54c 100644 --- a/shadowsocks-csharp/View/SubscribeForm.cs +++ b/shadowsocks-csharp/View/SubscribeForm.cs @@ -56,6 +56,14 @@ private void LoadCurrentConfiguration() { _modifiedConfiguration = controller.GetConfiguration(); LoadAllSettings(); + if (listServerSubscribe.Items.Count == 0) + { + textBoxURL.Enabled = false; + } + else + { + textBoxURL.Enabled = true; + } } private void LoadAllSettings() @@ -161,6 +169,8 @@ private void buttonAdd_Click(object sender, EventArgs e) UpdateList(); UpdateSelected(select_index); SetSelectIndex(select_index); + + textBoxURL.Enabled = true; } private void buttonDel_Click(object sender, EventArgs e) @@ -177,6 +187,10 @@ private void buttonDel_Click(object sender, EventArgs e) UpdateSelected(select_index); SetSelectIndex(select_index); } + if (listServerSubscribe.Items.Count == 0) + { + textBoxURL.Enabled = false; + } } } }