Posted by: Zeeshan Amjad | March 8, 2012

Disposable WeakReference


We already saw one example of Disposable GCHandle by writing one wrapper on top of GCHandle that implement IDisposable interface and Disposable pattern here. But one can argue that it is a structure, i.e. value type, and it is not allocated in heap, so there is less advantage of it. However WeakReference is a class written on top of GCHandle and it is allocated in managed heap. It means it perfectly make sense to write a wrapper on top of WeakReference that implement IDisposable interface and Disposable pattern.

We used the same technique here. which we used in implementing GCHandle wapper, here. Here is a class diagram of our wrapper to give an idea how we implement wrapper on top of every public method and properties on WeakReference class.

DisposableWeakReference

As we can see from this class diagram that we also implement ISerializable interface to make it as close to WeakReference as possible. Internaly GetObjectData method of ISerializable interface simply call the GEtObjectData method of WeakReference class. Here is a complete coding of our class.

Code Snippet
using System;
using System.Security.Permissions;
using System.Runtime.Serialization;

[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class DisposableWeakReference : IDisposable, ISerializable
{
    private WeakReference handle;

    public DisposableWeakReference(object target)
    {
        handle = new WeakReference(target);
    }

    public DisposableWeakReference(object target, bool trackResurrection)
    {
        handle = new WeakReference(target, trackResurrection);
    }
    #region ISerializable
    
    public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        handle.GetObjectData(info, context);
    }

    #endregion

    #region Properties

    public Object Target
    {
        get { return this.handle.Target; }
        set { this.handle.Target = value; }
    }

    public bool IsAlive
    {
        get { return this.handle.IsAlive; }
    }

    public bool TrackResurrection
    {
        get { return this.handle.TrackResurrection; }
    }

    #endregion

    #region DisposePattern

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.handle = null;
        }

        GC.SuppressFinalize(this);
    }

    public void Dispose()
    {
        this.Dispose(true);
    }

    public void Close()
    {
        this.Dispose(true);
    }

    ~DisposableWeakReference()
    {
        this.Dispose(false);
    }

    #endregion
}

 

Now we can use this class with using keyword of C# because it implements the IDisposable interface.

 

Code Snippet
Object obj = new Object();

using (DisposableWeakReference dwr = new DisposableWeakReference(obj))
{

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: