Skip to content

Commit 10702a0

Browse files
committed
Misc
Signed-off-by: Syrone Wong <[email protected]>
1 parent e099e92 commit 10702a0

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

shadowsocks-csharp/Util/SystemProxy/INTERNET_PER_CONN_OPTION.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public enum INTERNET_OPTION_PER_CONN_FLAGS_UI
7676
/// The StructLayout and FieldOffset attributes could help to decrease the struct size.
7777
/// </summary>
7878
[StructLayout(LayoutKind.Explicit)]
79-
public struct INTERNET_PER_CONN_OPTION_OptionUnion
79+
public struct INTERNET_PER_CONN_OPTION_OptionUnion : IDisposable
8080
{
8181
// A value in INTERNET_OPTION_PER_CONN_FLAGS.
8282
[FieldOffset(0)]
@@ -85,6 +85,24 @@ public struct INTERNET_PER_CONN_OPTION_OptionUnion
8585
public System.IntPtr pszValue;
8686
[FieldOffset(0)]
8787
public System.Runtime.InteropServices.ComTypes.FILETIME ftValue;
88+
89+
public void Dispose()
90+
{
91+
Dispose(true);
92+
GC.SuppressFinalize(this);
93+
}
94+
95+
private void Dispose(bool disposing)
96+
{
97+
if (disposing)
98+
{
99+
if (pszValue != IntPtr.Zero)
100+
{
101+
Marshal.FreeHGlobal(pszValue);
102+
pszValue = IntPtr.Zero;
103+
}
104+
}
105+
}
88106
}
89107

90108
[StructLayout(LayoutKind.Sequential)]

shadowsocks-csharp/Util/SystemProxy/INTERNET_PER_CONN_OPTION_LIST.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,47 @@ All other rights reserved.
1717
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
1818
\***************************************************************************/
1919

20+
using System;
2021
using System.Runtime.InteropServices;
2122

2223
namespace Shadowsocks.Util.SystemProxy
2324
{
2425
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
25-
public struct INTERNET_PER_CONN_OPTION_LIST
26+
public struct INTERNET_PER_CONN_OPTION_LIST : IDisposable
2627
{
2728
public int Size;
2829

2930
// The connection to be set. NULL means LAN.
3031
public System.IntPtr Connection;
31-
32+
3233
public int OptionCount;
3334
public int OptionError;
3435

3536
// List of INTERNET_PER_CONN_OPTIONs.
3637
public System.IntPtr pOptions;
38+
39+
public void Dispose()
40+
{
41+
Dispose( true );
42+
GC.SuppressFinalize( this );
43+
}
44+
45+
private void Dispose( bool disposing )
46+
{
47+
if ( disposing )
48+
{
49+
if ( Connection != IntPtr.Zero )
50+
{
51+
Marshal.FreeHGlobal( Connection );
52+
Connection = IntPtr.Zero;
53+
}
54+
55+
if ( pOptions != IntPtr.Zero )
56+
{
57+
Marshal.FreeHGlobal( pOptions );
58+
pOptions = IntPtr.Zero;
59+
}
60+
}
61+
}
3762
}
3863
}

shadowsocks-csharp/Util/SystemProxy/WinINet.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ public static void SetIEProxy(bool enable, bool global, string proxyServer, stri
121121
intptrStruct, optionListSize);
122122

123123
// Free the allocated memory.
124-
foreach (var v in _optionlist)
125-
{
126-
// free strings
127-
if (v.Value.pszValue != IntPtr.Zero)
128-
Marshal.FreeHGlobal(v.Value.pszValue);
129-
}
130124
Marshal.FreeCoTaskMem(buffer);
131125
Marshal.FreeCoTaskMem(intptrStruct);
132126

0 commit comments

Comments
 (0)