Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Dispose() to finalizers.

  • Loading branch information...
commit 9f62c1654cffae90ade76d348ce01b9a1297d3e9 1 parent 77396aa
Attila Kiskó enyim authored
72 Enyim.Caching/Memcached/MemcachedNode.cs
View
@@ -13,7 +13,7 @@ namespace Enyim.Caching.Memcached
/// Represents a Memcached node in the pool.
/// </summary>
[DebuggerDisplay("{{MemcachedNode [ Address: {EndPoint}, IsAlive = {IsAlive} ]}}")]
- public sealed class MemcachedNode : IDisposable, IMemcachedNode
+ public sealed class MemcachedNode : IMemcachedNode
{
private static readonly object SyncRoot = new Object();
@@ -109,11 +109,20 @@ public PooledSocket Acquire()
return this.internalPoolImpl.Acquire();
}
+
+ ~MemcachedNode()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
/// <summary>
/// Releases all resources allocated by this instance
/// </summary>
public void Dispose()
{
+ if (this.isDisposed) return;
+
GC.SuppressFinalize(this);
// this is not a graceful shutdown
@@ -122,8 +131,7 @@ public void Dispose()
// this should not kill any kittens
lock (SyncRoot)
{
- if (this.isDisposed)
- return;
+ if (this.isDisposed) return;
this.isDisposed = true;
@@ -363,45 +371,55 @@ private void ReleaseSocket(PooledSocket socket)
}
}
+
+ ~InternalPoolImpl()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
/// <summary>
/// Releases all resources allocated by this instance
/// </summary>
public void Dispose()
{
- // this is not a graceful shutdown
- // if someone uses a pooled item then 99% that an exception will be thrown
- // somewhere. But since the dispose is mostly used when everyone else is finished
- // this should not kill any kittens
- lock (this)
+ if (!this.isDisposed)
{
- this.CheckDisposed();
-
- this.isAlive = false;
- this.isDisposed = true;
-
- PooledSocket ps;
-
- while (this.freeItems.Dequeue(out ps))
+ // this is not a graceful shutdown
+ // if someone uses a pooled item then 99% that an exception will be thrown
+ // somewhere. But since the dispose is mostly used when everyone else is finished
+ // this should not kill any kittens
+ lock (this)
{
- try
+ if (!this.isDisposed)
{
- ps.OwnerNode = null;
- ps.Destroy();
+ this.isAlive = false;
+ this.isDisposed = true;
+
+ PooledSocket ps;
+
+ while (this.freeItems.Dequeue(out ps))
+ {
+ try
+ {
+ ps.OwnerNode = null;
+ ps.Destroy();
+ }
+ catch { }
+ }
+
+ this.ownerNode = null;
+ this.semaphore.Close();
+ this.semaphore = null;
+ this.freeItems = null;
}
- catch { }
}
-
- this.ownerNode = null;
- this.semaphore.Close();
- this.semaphore = null;
- this.freeItems = null;
}
}
private void CheckDisposed()
{
- if (this.isDisposed)
- throw new ObjectDisposedException("pool");
+ if (this.isDisposed) throw new ObjectDisposedException("pool");
}
void IDisposable.Dispose()
16 Enyim.Caching/Memcached/Operations/Binary/BinaryProtocol.cs
View
@@ -20,6 +20,12 @@ void IDisposable.Dispose()
this.Dispose();
}
+ ~BinaryProtocol()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
/// <summary>
/// Releases all resources allocated by this instance
/// </summary>
@@ -27,11 +33,11 @@ void IDisposable.Dispose()
/// the AppPool shuts down all resources will be released correctly and no handles or such will remain in the memory.</remarks>
public void Dispose()
{
- if (this.pool == null)
- throw new ObjectDisposedException("MemcachedClient");
-
- ((IDisposable)this.pool).Dispose();
- this.pool = null;
+ if (this.pool != null)
+ {
+ ((IDisposable)this.pool).Dispose();
+ this.pool = null;
+ }
}
private bool TryGet(string key, out object value)
6 Enyim.Caching/Memcached/Operations/Operation.cs
View
@@ -82,6 +82,12 @@ protected bool CheckDisposed(bool throwOnError)
return retval;
}
+ ~Operation()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
#region [ IDisposable ]
public virtual void Dispose()
{
17 Enyim.Caching/Memcached/Operations/Text/TextProtocol.cs
View
@@ -20,6 +20,13 @@ void IDisposable.Dispose()
this.Dispose();
}
+
+ ~TextProtocol()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
/// <summary>
/// Releases all resources allocated by this instance
/// </summary>
@@ -27,11 +34,11 @@ void IDisposable.Dispose()
/// the AppPool shuts down all resources will be released correctly and no handles or such will remain in the memory.</remarks>
public void Dispose()
{
- if (this.pool == null)
- throw new ObjectDisposedException("MemcachedClient");
-
- ((IDisposable)this.pool).Dispose();
- this.pool = null;
+ if (this.pool != null)
+ {
+ ((IDisposable)this.pool).Dispose();
+ this.pool = null;
+ }
}
bool IProtocolImplementation.Store(StoreMode mode, string key, object value, uint expires)
6 Enyim.Caching/Memcached/PooledSocket.cs
View
@@ -95,6 +95,12 @@ public bool IsAlive
this.Dispose(true);
}
+ ~PooledSocket()
+ {
+ try { this.Dispose(true); }
+ catch { }
+ }
+
protected void Dispose(bool disposing)
{
if (disposing)
6 Enyim.Caching/Memcached/ServerPool.cs
View
@@ -256,6 +256,12 @@ public ReadOnlyCollection<IMemcachedNode> WorkingServers
return keysByNode;
}
+ ~DefaultServerPool()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
#region [ IDisposable ]
void IDisposable.Dispose()
{
28 Enyim.Caching/MemcachedClient.cs
View
@@ -28,6 +28,12 @@ public MemcachedClient()
this.Initialize(DefaultSettings);
}
+ ~MemcachedClient()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
/// <summary>
/// Initializes a new MemcachedClient instance using the specified configuration section.
/// This overload allows to create multiple MemcachedClients with different pool configurations.
@@ -379,18 +385,18 @@ void IDisposable.Dispose()
/// the AppPool shuts down all resources will be released correctly and no handles or such will remain in the memory.</remarks>
public void Dispose()
{
- if (this.protImpl == null)
- throw new ObjectDisposedException("MemcachedClient");
-
- GC.SuppressFinalize(this);
-
- try
- {
- this.protImpl.Dispose();
- }
- finally
+ if (this.protImpl != null)
{
- this.protImpl = null;
+ GC.SuppressFinalize(this);
+
+ try
+ {
+ this.protImpl.Dispose();
+ }
+ finally
+ {
+ this.protImpl = null;
+ }
}
}
#endregion
6 Enyim.Caching/NorthScale.Store/NorthScalePool.cs
View
@@ -36,6 +36,12 @@ public NorthScalePool(INorthScaleClientConfiguration configuration, string bucke
this.keyTransformer = (IMemcachedKeyTransformer)Create(configuration.KeyTransformer) ?? new DefaultKeyTransformer();
}
+ ~NorthScalePool()
+ {
+ try { ((IDisposable)this).Dispose(); }
+ catch { }
+ }
+
private static object Create(Type type)
{
if (type == null) return null;
Please sign in to comment.
Something went wrong with that request. Please try again.