diff --git a/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs b/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs index 5d958a1a..033daf4c 100644 --- a/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs +++ b/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs @@ -15,9 +15,6 @@ namespace Shadowsocks.Encryption private IntPtr _encryptCtx = 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) : base(method, password, onetimeauth, isudp) { @@ -108,8 +105,12 @@ namespace Shadowsocks.Encryption } #region IDisposable + private bool _disposed; + // instance based lock + private readonly object _lock = new object(); + public override void Dispose() { Dispose(true); @@ -123,31 +124,32 @@ namespace Shadowsocks.Encryption protected virtual void Dispose(bool disposing) { - lock (_Lock) + lock (_lock) { - if (_disposed) - { - return; - } + if (_disposed) return; _disposed = true; } 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 } } diff --git a/shadowsocks-csharp/Util/ProcessManagement/Job.cs b/shadowsocks-csharp/Util/ProcessManagement/Job.cs index 098a3470..02646064 100644 --- a/shadowsocks-csharp/Util/ProcessManagement/Job.cs +++ b/shadowsocks-csharp/Util/ProcessManagement/Job.cs @@ -24,7 +24,7 @@ namespace Shadowsocks.Util.ProcessManagement [return: MarshalAs(UnmanagedType.Bool)] private static extern bool CloseHandle(IntPtr hObject); - private IntPtr handle; + private IntPtr handle = IntPtr.Zero; private bool disposed; public Job() @@ -70,19 +70,26 @@ namespace Shadowsocks.Util.ProcessManagement private void Dispose(bool disposing) { - if (disposed) - return; + if (disposed) return; + disposed = true; if (disposing) { } 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) diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index ce63734d..41cd73db 100644 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -501,12 +501,14 @@ namespace Shadowsocks.View void logForm_FormClosed(object sender, FormClosedEventArgs e) { + logForm.Dispose(); logForm = null; Utils.ReleaseMemory(true); } void configForm_FormClosed(object sender, FormClosedEventArgs e) { + configForm.Dispose(); configForm = null; Utils.ReleaseMemory(true); if (_isFirstRun) @@ -519,12 +521,14 @@ namespace Shadowsocks.View void proxyForm_FormClosed(object sender, FormClosedEventArgs e) { + proxyForm.Dispose(); proxyForm = null; Utils.ReleaseMemory(true); } void hotkeySettingsForm_FormClosed(object sender, FormClosedEventArgs e) { + hotkeySettingsForm.Dispose(); hotkeySettingsForm = null; Utils.ReleaseMemory(true); }