Skip to content

Commit

Permalink
fix mem leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
breakwa11 committed Jul 25, 2017
1 parent 98d3c36 commit 53a7ccb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 77 deletions.
2 changes: 1 addition & 1 deletion shadowsocks-csharp/Controller/SystemProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 15 additions & 9 deletions shadowsocks-csharp/Util/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static LRUCache<string, IPAddress> DnsBuffer
}
}

static Process current_process = Process.GetCurrentProcess();

public static void ReleaseMemory()
{
#if !_CONSOLE
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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")]
Expand Down
10 changes: 6 additions & 4 deletions shadowsocks-csharp/View/LogForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
132 changes: 69 additions & 63 deletions shadowsocks-csharp/View/MenuViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++)
Expand All @@ -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 ?
Expand Down Expand Up @@ -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();
}
}
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions shadowsocks-csharp/View/SubscribeForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}
}
}

0 comments on commit 53a7ccb

Please sign in to comment.