Adjust garbage collection
Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
CloseHandle(handle);
|
||||
handle = IntPtr.Zero;
|
||||
if (handle != IntPtr.Zero)
|
||||
{
|
||||
CloseHandle(handle);
|
||||
handle = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
~Job()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public bool AddProcess(IntPtr processHandle)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user