Skip to content

Commit 53a7ccb

Browse files
committed
fix mem leaks
1 parent 98d3c36 commit 53a7ccb

File tree

5 files changed

+105
-77
lines changed

5 files changed

+105
-77
lines changed

shadowsocks-csharp/Controller/SystemProxy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private static void SetIEProxy(bool enable, bool global, string proxyServer, str
240240
foreach (INTERNET_PER_CONN_OPTION eachOption in _optionlist)
241241
{
242242
Marshal.StructureToPtr(eachOption, current, false);
243-
current = (IntPtr)((int)current + Marshal.SizeOf(eachOption));
243+
current = (IntPtr)((long)current + Marshal.SizeOf(eachOption));
244244
}
245245

246246
// Initialize a INTERNET_PER_CONN_OPTION_LIST instance.

shadowsocks-csharp/Util/Util.cs

+15-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public static LRUCache<string, IPAddress> DnsBuffer
3131
}
3232
}
3333

34+
static Process current_process = Process.GetCurrentProcess();
35+
3436
public static void ReleaseMemory()
3537
{
3638
#if !_CONSOLE
@@ -45,13 +47,13 @@ public static void ReleaseMemory()
4547

4648
if (UIntPtr.Size == 4)
4749
{
48-
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle,
50+
SetProcessWorkingSetSize(current_process.Handle,
4951
(UIntPtr)0xFFFFFFFF,
5052
(UIntPtr)0xFFFFFFFF);
5153
}
5254
else if (UIntPtr.Size == 8)
5355
{
54-
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle,
56+
SetProcessWorkingSetSize(current_process.Handle,
5557
(UIntPtr)0xFFFFFFFFFFFFFFFF,
5658
(UIntPtr)0xFFFFFFFFFFFFFFFF);
5759
}
@@ -464,8 +466,10 @@ public static int RunAsAdmin(string Arguments)
464466
public static int GetDpiMul()
465467
{
466468
int dpi;
467-
Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
468-
dpi = (int)graphics.DpiX;
469+
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
470+
{
471+
dpi = (int)graphics.DpiX;
472+
}
469473
return (dpi * 4 + 48) / 96;
470474
}
471475

@@ -478,12 +482,14 @@ public enum DeviceCap
478482

479483
public static Point GetScreenPhysicalSize()
480484
{
481-
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
482-
IntPtr desktop = g.GetHdc();
483-
int PhysicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPHORZRES);
484-
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
485+
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
486+
{
487+
IntPtr desktop = g.GetHdc();
488+
int PhysicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPHORZRES);
489+
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
485490

486-
return new Point(PhysicalScreenWidth, PhysicalScreenHeight);
491+
return new Point(PhysicalScreenWidth, PhysicalScreenHeight);
492+
}
487493
}
488494

489495
[DllImport("gdi32.dll")]

shadowsocks-csharp/View/LogForm.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,13 @@ private void LogForm_Shown(object sender, EventArgs e)
129129

130130
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
131131
{
132-
var fontDialog = new FontDialog();
133-
fontDialog.Font = logTextBox.Font;
134-
if (fontDialog.ShowDialog() == DialogResult.OK)
132+
using (FontDialog fontDialog = new FontDialog())
135133
{
136-
logTextBox.Font = fontDialog.Font;
134+
fontDialog.Font = logTextBox.Font;
135+
if (fontDialog.ShowDialog() == DialogResult.OK)
136+
{
137+
logTextBox.Font = fontDialog.Font;
138+
}
137139
}
138140
}
139141

shadowsocks-csharp/View/MenuViewController.cs

+69-63
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,16 @@ private void UpdateTrayIcon()
135135
bool global = config.sysProxyMode == (int)ProxyMode.Global;
136136
bool random = config.random;
137137

138-
Bitmap icon = null;
139138
try
140139
{
141-
icon = new Bitmap("icon.png");
140+
using (Bitmap icon = new Bitmap("icon.png"))
141+
{
142+
_notifyIcon.Icon = Icon.FromHandle(icon.GetHicon());
143+
}
142144
}
143145
catch
144146
{
147+
Bitmap icon = null;
145148
if (dpi < 97)
146149
{
147150
// dpi = 96;
@@ -171,8 +174,8 @@ private void UpdateTrayIcon()
171174
mul_r = 0.4;
172175
}
173176

177+
using (Bitmap iconCopy = new Bitmap(icon))
174178
{
175-
Bitmap iconCopy = new Bitmap(icon);
176179
for (int x = 0; x < iconCopy.Width; x++)
177180
{
178181
for (int y = 0; y < iconCopy.Height; y++)
@@ -185,10 +188,9 @@ private void UpdateTrayIcon()
185188
((byte)(color.B * mul_b))));
186189
}
187190
}
188-
icon = iconCopy;
191+
_notifyIcon.Icon = Icon.FromHandle(iconCopy.GetHicon());
189192
}
190193
}
191-
_notifyIcon.Icon = Icon.FromHandle(icon.GetHicon());
192194

193195
// we want to show more details but notify icon title is limited to 63 characters
194196
string text = (enabled ?
@@ -857,20 +859,22 @@ private void Config_Click(object sender, EventArgs e)
857859

858860
private void Import_Click(object sender, EventArgs e)
859861
{
860-
OpenFileDialog dlg = new OpenFileDialog();
861-
dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath;
862-
if (dlg.ShowDialog() == DialogResult.OK)
862+
using (OpenFileDialog dlg = new OpenFileDialog())
863863
{
864-
string name = dlg.FileName;
865-
Configuration cfg = Configuration.LoadFile(name);
866-
if (cfg.configs.Count == 1 && cfg.configs[0].server == Configuration.GetDefaultServer().server)
867-
{
868-
MessageBox.Show("Load config file failed", "ShadowsocksR");
869-
}
870-
else
864+
dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath;
865+
if (dlg.ShowDialog() == DialogResult.OK)
871866
{
872-
controller.MergeConfiguration(cfg);
873-
LoadCurrentConfiguration();
867+
string name = dlg.FileName;
868+
Configuration cfg = Configuration.LoadFile(name);
869+
if (cfg.configs.Count == 1 && cfg.configs[0].server == Configuration.GetDefaultServer().server)
870+
{
871+
MessageBox.Show("Load config file failed", "ShadowsocksR");
872+
}
873+
else
874+
{
875+
controller.MergeConfiguration(cfg);
876+
LoadCurrentConfiguration();
877+
}
874878
}
875879
}
876880
}
@@ -1169,32 +1173,33 @@ private void CopyAddress_Click(object sender, EventArgs e)
11691173

11701174
private bool ScanQRCode(Screen screen, Bitmap fullImage, Rectangle cropRect, out string url, out Rectangle rect)
11711175
{
1172-
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
1173-
1174-
using (Graphics g = Graphics.FromImage(target))
1175-
{
1176-
g.DrawImage(fullImage, new Rectangle(0, 0, cropRect.Width, cropRect.Height),
1177-
cropRect,
1178-
GraphicsUnit.Pixel);
1179-
}
1180-
var source = new BitmapLuminanceSource(target);
1181-
var bitmap = new BinaryBitmap(new HybridBinarizer(source));
1182-
QRCodeReader reader = new QRCodeReader();
1183-
var result = reader.decode(bitmap);
1184-
if (result != null)
1176+
using (Bitmap target = new Bitmap(cropRect.Width, cropRect.Height))
11851177
{
1186-
url = result.Text;
1187-
double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0;
1188-
foreach (ResultPoint point in result.ResultPoints)
1178+
using (Graphics g = Graphics.FromImage(target))
1179+
{
1180+
g.DrawImage(fullImage, new Rectangle(0, 0, cropRect.Width, cropRect.Height),
1181+
cropRect,
1182+
GraphicsUnit.Pixel);
1183+
}
1184+
var source = new BitmapLuminanceSource(target);
1185+
var bitmap = new BinaryBitmap(new HybridBinarizer(source));
1186+
QRCodeReader reader = new QRCodeReader();
1187+
var result = reader.decode(bitmap);
1188+
if (result != null)
11891189
{
1190-
minX = Math.Min(minX, point.X);
1191-
minY = Math.Min(minY, point.Y);
1192-
maxX = Math.Max(maxX, point.X);
1193-
maxY = Math.Max(maxY, point.Y);
1190+
url = result.Text;
1191+
double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0;
1192+
foreach (ResultPoint point in result.ResultPoints)
1193+
{
1194+
minX = Math.Min(minX, point.X);
1195+
minY = Math.Min(minY, point.Y);
1196+
maxX = Math.Max(maxX, point.X);
1197+
maxY = Math.Max(maxY, point.Y);
1198+
}
1199+
//rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1200+
rect = new Rectangle(cropRect.Left + (int)minX, cropRect.Top + (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1201+
return true;
11941202
}
1195-
//rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1196-
rect = new Rectangle(cropRect.Left + (int)minX, cropRect.Top + (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1197-
return true;
11981203
}
11991204
url = "";
12001205
rect = new Rectangle();
@@ -1203,32 +1208,33 @@ private bool ScanQRCode(Screen screen, Bitmap fullImage, Rectangle cropRect, out
12031208

12041209
private bool ScanQRCodeStretch(Screen screen, Bitmap fullImage, Rectangle cropRect, double mul, out string url, out Rectangle rect)
12051210
{
1206-
Bitmap target = new Bitmap((int)(cropRect.Width * mul), (int)(cropRect.Height * mul));
1207-
1208-
using (Graphics g = Graphics.FromImage(target))
1211+
using (Bitmap target = new Bitmap((int)(cropRect.Width * mul), (int)(cropRect.Height * mul)))
12091212
{
1210-
g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height),
1211-
cropRect,
1212-
GraphicsUnit.Pixel);
1213-
}
1214-
var source = new BitmapLuminanceSource(target);
1215-
var bitmap = new BinaryBitmap(new HybridBinarizer(source));
1216-
QRCodeReader reader = new QRCodeReader();
1217-
var result = reader.decode(bitmap);
1218-
if (result != null)
1219-
{
1220-
url = result.Text;
1221-
double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0;
1222-
foreach (ResultPoint point in result.ResultPoints)
1213+
using (Graphics g = Graphics.FromImage(target))
1214+
{
1215+
g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height),
1216+
cropRect,
1217+
GraphicsUnit.Pixel);
1218+
}
1219+
var source = new BitmapLuminanceSource(target);
1220+
var bitmap = new BinaryBitmap(new HybridBinarizer(source));
1221+
QRCodeReader reader = new QRCodeReader();
1222+
var result = reader.decode(bitmap);
1223+
if (result != null)
12231224
{
1224-
minX = Math.Min(minX, point.X);
1225-
minY = Math.Min(minY, point.Y);
1226-
maxX = Math.Max(maxX, point.X);
1227-
maxY = Math.Max(maxY, point.Y);
1225+
url = result.Text;
1226+
double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0;
1227+
foreach (ResultPoint point in result.ResultPoints)
1228+
{
1229+
minX = Math.Min(minX, point.X);
1230+
minY = Math.Min(minY, point.Y);
1231+
maxX = Math.Max(maxX, point.X);
1232+
maxY = Math.Max(maxY, point.Y);
1233+
}
1234+
//rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1235+
rect = new Rectangle(cropRect.Left + (int)(minX / mul), cropRect.Top + (int)(minY / mul), (int)((maxX - minX) / mul), (int)((maxY - minY) / mul));
1236+
return true;
12281237
}
1229-
//rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1230-
rect = new Rectangle(cropRect.Left + (int)(minX / mul), cropRect.Top + (int)(minY / mul), (int)((maxX - minX) / mul), (int)((maxY - minY) / mul));
1231-
return true;
12321238
}
12331239
url = "";
12341240
rect = new Rectangle();

shadowsocks-csharp/View/SubscribeForm.cs

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ private void LoadCurrentConfiguration()
5656
{
5757
_modifiedConfiguration = controller.GetConfiguration();
5858
LoadAllSettings();
59+
if (listServerSubscribe.Items.Count == 0)
60+
{
61+
textBoxURL.Enabled = false;
62+
}
63+
else
64+
{
65+
textBoxURL.Enabled = true;
66+
}
5967
}
6068

6169
private void LoadAllSettings()
@@ -161,6 +169,8 @@ private void buttonAdd_Click(object sender, EventArgs e)
161169
UpdateList();
162170
UpdateSelected(select_index);
163171
SetSelectIndex(select_index);
172+
173+
textBoxURL.Enabled = true;
164174
}
165175

166176
private void buttonDel_Click(object sender, EventArgs e)
@@ -177,6 +187,10 @@ private void buttonDel_Click(object sender, EventArgs e)
177187
UpdateSelected(select_index);
178188
SetSelectIndex(select_index);
179189
}
190+
if (listServerSubscribe.Items.Count == 0)
191+
{
192+
textBoxURL.Enabled = false;
193+
}
180194
}
181195
}
182196
}

0 commit comments

Comments
 (0)