Signed-off-by: Syrone Wong <wong.syrone@gmail.com>tags/3.3.2
@@ -15,9 +15,6 @@ namespace Shadowsocks.Encryption | |||||
private IntPtr _encryptCtx = IntPtr.Zero; | private IntPtr _encryptCtx = IntPtr.Zero; | ||||
private IntPtr _decryptCtx = IntPtr.Zero; | private IntPtr _decryptCtx = IntPtr.Zero; | ||||
// instance based lock | |||||
private readonly object _Lock = new object(); | |||||
public MbedTLSEncryptor(string method, string password, bool onetimeauth, bool isudp) | public MbedTLSEncryptor(string method, string password, bool onetimeauth, bool isudp) | ||||
: base(method, password, onetimeauth, isudp) | : base(method, password, onetimeauth, isudp) | ||||
{ | { | ||||
@@ -108,8 +105,12 @@ namespace Shadowsocks.Encryption | |||||
} | } | ||||
#region IDisposable | #region IDisposable | ||||
private bool _disposed; | private bool _disposed; | ||||
// instance based lock | |||||
private readonly object _lock = new object(); | |||||
public override void Dispose() | public override void Dispose() | ||||
{ | { | ||||
Dispose(true); | Dispose(true); | ||||
@@ -123,31 +124,32 @@ namespace Shadowsocks.Encryption | |||||
protected virtual void Dispose(bool disposing) | protected virtual void Dispose(bool disposing) | ||||
{ | { | ||||
lock (_Lock) | |||||
lock (_lock) | |||||
{ | { | ||||
if (_disposed) | |||||
{ | |||||
return; | |||||
} | |||||
if (_disposed) return; | |||||
_disposed = true; | _disposed = true; | ||||
} | } | ||||
if (disposing) | if (disposing) | ||||
{ | { | ||||
if (_encryptCtx != IntPtr.Zero) | |||||
{ | |||||
MbedTLS.cipher_free(_encryptCtx); | |||||
Marshal.FreeHGlobal(_encryptCtx); | |||||
_encryptCtx = IntPtr.Zero; | |||||
} | |||||
if (_decryptCtx != IntPtr.Zero) | |||||
{ | |||||
MbedTLS.cipher_free(_decryptCtx); | |||||
Marshal.FreeHGlobal(_decryptCtx); | |||||
_decryptCtx = IntPtr.Zero; | |||||
} | |||||
// free managed objects | |||||
} | |||||
// free unmanaged objects | |||||
if (_encryptCtx != IntPtr.Zero) | |||||
{ | |||||
MbedTLS.cipher_free(_encryptCtx); | |||||
Marshal.FreeHGlobal(_encryptCtx); | |||||
_encryptCtx = IntPtr.Zero; | |||||
} | |||||
if (_decryptCtx != IntPtr.Zero) | |||||
{ | |||||
MbedTLS.cipher_free(_decryptCtx); | |||||
Marshal.FreeHGlobal(_decryptCtx); | |||||
_decryptCtx = IntPtr.Zero; | |||||
} | } | ||||
} | } | ||||
#endregion | #endregion | ||||
} | } | ||||
} | } |
@@ -24,7 +24,7 @@ namespace Shadowsocks.Util.ProcessManagement | |||||
[return: MarshalAs(UnmanagedType.Bool)] | [return: MarshalAs(UnmanagedType.Bool)] | ||||
private static extern bool CloseHandle(IntPtr hObject); | private static extern bool CloseHandle(IntPtr hObject); | ||||
private IntPtr handle; | |||||
private IntPtr handle = IntPtr.Zero; | |||||
private bool disposed; | private bool disposed; | ||||
public Job() | public Job() | ||||
@@ -70,19 +70,26 @@ namespace Shadowsocks.Util.ProcessManagement | |||||
private void Dispose(bool disposing) | private void Dispose(bool disposing) | ||||
{ | { | ||||
if (disposed) | |||||
return; | |||||
if (disposed) return; | |||||
disposed = true; | |||||
if (disposing) { } | if (disposing) { } | ||||
Close(); | Close(); | ||||
disposed = true; | |||||
} | } | ||||
public void Close() | |||||
private void Close() | |||||
{ | |||||
if (handle != IntPtr.Zero) | |||||
{ | |||||
CloseHandle(handle); | |||||
handle = IntPtr.Zero; | |||||
} | |||||
} | |||||
~Job() | |||||
{ | { | ||||
CloseHandle(handle); | |||||
handle = IntPtr.Zero; | |||||
Dispose(false); | |||||
} | } | ||||
public bool AddProcess(IntPtr processHandle) | public bool AddProcess(IntPtr processHandle) | ||||
@@ -501,12 +501,14 @@ namespace Shadowsocks.View | |||||
void logForm_FormClosed(object sender, FormClosedEventArgs e) | void logForm_FormClosed(object sender, FormClosedEventArgs e) | ||||
{ | { | ||||
logForm.Dispose(); | |||||
logForm = null; | logForm = null; | ||||
Utils.ReleaseMemory(true); | Utils.ReleaseMemory(true); | ||||
} | } | ||||
void configForm_FormClosed(object sender, FormClosedEventArgs e) | void configForm_FormClosed(object sender, FormClosedEventArgs e) | ||||
{ | { | ||||
configForm.Dispose(); | |||||
configForm = null; | configForm = null; | ||||
Utils.ReleaseMemory(true); | Utils.ReleaseMemory(true); | ||||
if (_isFirstRun) | if (_isFirstRun) | ||||
@@ -519,12 +521,14 @@ namespace Shadowsocks.View | |||||
void proxyForm_FormClosed(object sender, FormClosedEventArgs e) | void proxyForm_FormClosed(object sender, FormClosedEventArgs e) | ||||
{ | { | ||||
proxyForm.Dispose(); | |||||
proxyForm = null; | proxyForm = null; | ||||
Utils.ReleaseMemory(true); | Utils.ReleaseMemory(true); | ||||
} | } | ||||
void hotkeySettingsForm_FormClosed(object sender, FormClosedEventArgs e) | void hotkeySettingsForm_FormClosed(object sender, FormClosedEventArgs e) | ||||
{ | { | ||||
hotkeySettingsForm.Dispose(); | |||||
hotkeySettingsForm = null; | hotkeySettingsForm = null; | ||||
Utils.ReleaseMemory(true); | Utils.ReleaseMemory(true); | ||||
} | } | ||||