Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit ce84f05

Browse files
committed
#740 use destructor as safety net for dispose
1 parent 2ce70ef commit ce84f05

File tree

13 files changed

+249
-100
lines changed

13 files changed

+249
-100
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ Supports
3939

4040
* Fix [outstanding bugs](https://github.com/justcoding121/Titanium-Web-Proxy/issues?q=is%3Aopen+is%3Aissue+label%3Abug)
4141
* Support reading request and response body as stream [#823](https://github.com/justcoding121/Titanium-Web-Proxy/issues/823)
42-
* Use Dispose(false) pattern to reduce possibility of memory leaks [#740](https://github.com/justcoding121/Titanium-Web-Proxy/issues/740)
4342
* Stop throwing new exceptions [#634](https://github.com/justcoding121/Titanium-Web-Proxy/issues/634)
4443
* Support HTTP 2.0
4544

src/Titanium.Web.Proxy/Certificates/CertificateManager.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,6 @@ public ICertificateCache CertificateStorage
283283
/// </summary>
284284
public X509KeyStorageFlags StorageFlag { get; set; } = X509KeyStorageFlags.Exportable;
285285

286-
/// <summary>
287-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
288-
/// </summary>
289-
public void Dispose()
290-
{
291-
clearCertificatesTokenSource.Dispose();
292-
}
293-
294286
/// <summary>
295287
/// For CertificateEngine.DefaultWindows to work we need to also check in personal store
296288
/// </summary>
@@ -1000,5 +992,29 @@ public void ClearRootCertificate()
1000992
cachedCertificates.Clear();
1001993
rootCertificate = null;
1002994
}
995+
996+
private bool disposed = false;
997+
998+
void dispose(bool disposing)
999+
{
1000+
if (disposed)
1001+
{
1002+
return;
1003+
}
1004+
1005+
clearCertificatesTokenSource.Dispose();
1006+
disposed = true;
1007+
}
1008+
1009+
public void Dispose()
1010+
{
1011+
dispose(true);
1012+
GC.SuppressFinalize(this);
1013+
}
1014+
1015+
~CertificateManager()
1016+
{
1017+
dispose(false);
1018+
}
10031019
}
10041020
}

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,23 @@ public void TerminateServerConnection()
665665
HttpClient.CloseServerConnection = true;
666666
}
667667

668-
/// <summary>
669-
/// Implement any cleanup here
670-
/// </summary>
671-
public override void Dispose()
668+
private bool disposed = false;
669+
protected override void Dispose(bool disposing)
672670
{
671+
if (disposed)
672+
{
673+
return;
674+
}
675+
673676
MultipartRequestPartSent = null;
674-
base.Dispose();
677+
disposed = true;
678+
679+
base.Dispose(disposing);
680+
}
681+
682+
~SessionEventArgs()
683+
{
684+
Dispose(false);
675685
}
676686
}
677687
}

src/Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,39 @@ public bool EnableWinAuth
150150
/// </summary>
151151
public Exception? Exception { get; internal set; }
152152

153-
/// <summary>
154-
/// Implements cleanup here.
155-
/// </summary>
156-
public virtual void Dispose()
153+
private bool disposed = false;
154+
155+
protected virtual void Dispose(bool disposing)
157156
{
158-
CustomUpStreamProxyUsed = null;
157+
if (disposed)
158+
{
159+
return;
160+
}
161+
162+
disposed = true;
163+
164+
if (disposing)
165+
{
166+
CustomUpStreamProxyUsed = null;
159167

160-
DataSent = null;
161-
DataReceived = null;
162-
Exception = null;
168+
DataSent = null;
169+
DataReceived = null;
170+
Exception = null;
171+
}
163172

164173
HttpClient.FinishSession();
174+
175+
}
176+
177+
public void Dispose()
178+
{
179+
Dispose(true);
180+
GC.SuppressFinalize(this);
181+
}
182+
183+
~SessionEventArgsBase()
184+
{
185+
Dispose(false);
165186
}
166187

167188
/// <summary>

src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public WinHttpWebProxyFinder()
4848

4949
private WebProxy? proxy { get; set; }
5050

51-
public void Dispose()
52-
{
53-
dispose(true);
54-
}
55-
5651
public bool GetAutoProxies(Uri destination, out IList<string>? proxyList)
5752
{
5853
proxyList = null;
@@ -191,16 +186,6 @@ public void Reset()
191186
autoDetectFailed = false;
192187
}
193188

194-
private void dispose(bool disposing)
195-
{
196-
if (!disposing || session == null || session.IsInvalid)
197-
{
198-
return;
199-
}
200-
201-
session.Close();
202-
}
203-
204189
private int getAutoProxies(Uri destination, Uri? scriptLocation, out string? proxyListString)
205190
{
206191
int num = 0;
@@ -352,5 +337,35 @@ private enum AutoWebProxyState
352337
UnrecognizedScheme,
353338
Completed
354339
}
340+
341+
private bool disposed = false;
342+
343+
void dispose(bool disposing)
344+
{
345+
if (disposed)
346+
{
347+
return;
348+
}
349+
350+
disposed = true;
351+
352+
if (session == null || session.IsInvalid)
353+
{
354+
return;
355+
}
356+
357+
session.Close();
358+
}
359+
360+
public void Dispose()
361+
{
362+
dispose(true);
363+
GC.SuppressFinalize(this);
364+
}
365+
366+
~WinHttpWebProxyFinder()
367+
{
368+
dispose(false);
369+
}
355370
}
356371
}

src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ public int GetProcessId(ProxyEndPoint endPoint)
7373
throw new PlatformNotSupportedException();
7474
}
7575

76-
/// <summary>
77-
/// Dispose.
78-
/// </summary>
79-
public void Dispose()
76+
private bool disposed = false;
77+
78+
protected virtual void Dispose(bool disposing)
8079
{
80+
if (disposed)
81+
{
82+
return;
83+
}
84+
8185
Task.Run(async () =>
8286
{
8387
// delay calling tcp connection close()
@@ -95,6 +99,19 @@ public void Dispose()
9599
// ignore
96100
}
97101
});
102+
103+
disposed = true;
104+
}
105+
106+
public void Dispose()
107+
{
108+
Dispose(true);
109+
GC.SuppressFinalize(this);
110+
}
111+
112+
~TcpClientConnection()
113+
{
114+
Dispose(false);
98115
}
99116
}
100117
}

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,15 @@ private async Task clearOutdatedConnections()
758758
}
759759
}
760760

761-
public void Dispose()
761+
private bool disposed = false;
762+
763+
protected virtual void Dispose(bool disposing)
762764
{
765+
if (disposed)
766+
{
767+
return;
768+
}
769+
763770
runCleanUpTask = false;
764771

765772
try
@@ -791,6 +798,19 @@ public void Dispose()
791798
connection?.Dispose();
792799
}
793800
}
801+
802+
disposed = true;
803+
}
804+
805+
public void Dispose()
806+
{
807+
Dispose(true);
808+
GC.SuppressFinalize(this);
809+
}
810+
811+
~TcpConnectionFactory()
812+
{
813+
Dispose(false);
794814
}
795815

796816
static class SocketConnectionTaskFactory

src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,15 @@ internal TcpServerConnection(ProxyServer proxyServer, Socket tcpSocket, HttpServ
8484
/// </summary>
8585
internal bool IsWinAuthenticated { get; set; }
8686

87-
/// <summary>
88-
/// Dispose.
89-
/// </summary>
90-
public void Dispose()
87+
private bool disposed = false;
88+
89+
protected virtual void Dispose(bool disposing)
9190
{
91+
if (disposed)
92+
{
93+
return;
94+
}
95+
9296
Task.Run(async () =>
9397
{
9498
// delay calling tcp connection close()
@@ -108,6 +112,18 @@ public void Dispose()
108112
}
109113
});
110114

115+
disposed = true;
116+
}
117+
118+
public void Dispose()
119+
{
120+
Dispose(true);
121+
GC.SuppressFinalize(this);
122+
}
123+
124+
~TcpServerConnection()
125+
{
126+
Dispose(false);
111127
}
112128
}
113129
}

src/Titanium.Web.Proxy/Network/WinAuth/Security/Common.cs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ internal SecurityInteger(int dummy)
129129
}
130130

131131
[StructLayout(LayoutKind.Sequential)]
132-
internal struct SecurityBuffer : IDisposable
132+
internal struct SecurityBuffer
133133
{
134134
internal int cbBuffer;
135135
internal int cbBufferType;
@@ -158,18 +158,10 @@ internal SecurityBuffer(byte[] secBufferBytes, SecurityBufferType bufferType)
158158
Marshal.Copy(secBufferBytes, 0, pvBuffer, cbBuffer);
159159
}
160160

161-
public void Dispose()
162-
{
163-
if (pvBuffer != IntPtr.Zero)
164-
{
165-
Marshal.FreeHGlobal(pvBuffer);
166-
pvBuffer = IntPtr.Zero;
167-
}
168-
}
169161
}
170162

171163
[StructLayout(LayoutKind.Sequential)]
172-
internal struct SecurityBufferDesciption : IDisposable
164+
internal struct SecurityBufferDesciption
173165
{
174166
internal int ulVersion;
175167
internal int cBuffers;
@@ -193,36 +185,7 @@ internal SecurityBufferDesciption(byte[] secBufferBytes)
193185
Marshal.StructureToPtr(thisSecBuffer, pBuffers, false);
194186
}
195187

196-
public void Dispose()
197-
{
198-
if (pBuffers != IntPtr.Zero)
199-
{
200-
if (cBuffers == 1)
201-
{
202-
var thisSecBuffer = (SecurityBuffer)Marshal.PtrToStructure(pBuffers, typeof(SecurityBuffer));
203-
thisSecBuffer.Dispose();
204-
}
205-
else
206-
{
207-
for (int index = 0; index < cBuffers; index++)
208-
{
209-
// The bits were written out the following order:
210-
// int cbBuffer;
211-
// int BufferType;
212-
// pvBuffer;
213-
// What we need to do here is to grab a hold of the pvBuffer allocate by the individual
214-
// SecBuffer and release it...
215-
int currentOffset = index * Marshal.SizeOf(typeof(Buffer));
216-
var secBufferpvBuffer = Marshal.ReadIntPtr(pBuffers,
217-
currentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int)));
218-
Marshal.FreeHGlobal(secBufferpvBuffer);
219-
}
220-
}
221-
222-
Marshal.FreeHGlobal(pBuffers);
223-
pBuffers = IntPtr.Zero;
224-
}
225-
}
188+
226189

227190
internal byte[]? GetBytes()
228191
{

0 commit comments

Comments
 (0)