Click here to Skip to main content
15,886,518 members
Articles / Containers / Virtual Machine

Observable property pattern, memory leaks, and weak delegates for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (42 votes)
4 Dec 200624 min read 217.6K   359   174  
This article is dedicated to the observable property design pattern, a very nice pattern used in the Microsoft .NET Framework, a possible memory leak problem with it, and gives a couple of ways to solve it.
using System;
using System.ComponentModel;
using System.Drawing;

namespace Pvax.WeakDelegates.Tests
{
	/// <summary>
	/// A mock object that is a source of all test events.
	/// </summary>
	/// <remarks>
	/// For each {Event} there is an On{Event} method to be called to raise the
	/// {Event}.
	/// </remarks>
	public class EventSource: Constants
	{
		public event EventHandler Event;

		public void OnEvent(EventArgs e)
		{
			EventHandler handler = Event;
			if(null != handler)
				handler(this, e);
		}

		public event CustomEventHandler CustomEvent;

		public void OnCustomEvent(CustomEventArgs e)
		{
			Pvax.WeakDelegates.Tests.CustomEvent.Raise(CustomEvent, this, e);
		}

		public event VoidHandler VoidEvent;

		public void OnVoidEvent()
		{
			if(VoidEvent != null)
			{
				VoidEvent();
			}
		}

		public event VoidHandler2 Void2Event;

		public void OnVoid2Event(EventArgs e)
		{
			if(Void2Event != null)
			{
				Void2Event(this, e);
			}
		}

		public event VoidHandler5 Void5Event;

		public void OnVoid5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(Void5Event != null)
			{
				Void5Event(this, arg2, arg3, arg4, arg5);
			}
		}

		public event StringHandler StringEvent;

		public string OnStringEvent()
		{
			if(StringEvent != null)
			{
				return StringEvent();
			}
			return String.Empty;
		}

		public event StringHandler2 String2Event;

		public string OnString2Event(EventArgs e)
		{
			if(String2Event != null)
			{
				return String2Event(this, e);
			}
			return String.Empty;
		}

		public event StringHandler5 String5Event;

		public string OnString5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(String5Event != null)
			{
				return String5Event(this, arg2, arg3, arg4, arg5);
			}
			return String.Empty;
		}

		public event DoubleHandler DoubleEvent;

		public double OnDoubleEvent()
		{
			if(DoubleEvent != null)
			{
				return DoubleEvent();
			}
			return Double.NaN;
		}

		public event DoubleHandler2 Double2Event;

		public double OnDouble2Event(EventArgs e)
		{
			if(Double2Event != null)
			{
				return Double2Event(this, e);
			}
			return Double.NaN;
		}

		public event DoubleHandler5 Double5Event;

		public double OnDouble5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(Double5Event != null)
			{
				return Double5Event(this, arg2, arg3, arg4, arg5);
			}
			return Double.NaN;
		}

		public event PointFHandler PointFEvent;

		public PointF OnPointFEvent()
		{
			if(PointFEvent != null)
			{
				return PointFEvent();
			}
			return new PointF(float.NaN, float.NaN);
		}

		public event PointFHandler2 PointF2Event;

		public PointF OnPointF2Event(EventArgs e)
		{
			if(PointF2Event != null)
			{
				return PointF2Event(this, e);
			}
			return new PointF(float.NaN, float.NaN);
		}

		public event PointFHandler5 PointF5Event;

		public PointF OnPointF5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(PointF5Event != null)
			{
				return PointF5Event(this, arg2, arg3, arg4, arg5);
			}
			return new PointF(float.NaN, float.NaN);
		}


		public static event EventHandler StaticEvent;

		public static void OnStaticEvent(EventArgs e)
		{
			if(StaticEvent != null)
			{
				StaticEvent(null, e);
			}
		}

		public static event CustomEventHandler StaticCustomEvent;

		public static void OnStaticCustomEvent(CustomEventArgs e)
		{
			Pvax.WeakDelegates.Tests.CustomEvent.Raise(StaticCustomEvent, null, e);
		}

		public static event VoidHandler StaticVoidEvent;

		public static void OnStaticVoidEvent()
		{
			if(StaticVoidEvent != null)
			{
				StaticVoidEvent();
			}
		}

		public static event VoidHandler2 StaticVoid2Event;

		public static void OnStaticVoid2Event(EventArgs e)
		{
			if(StaticVoid2Event != null)
			{
				StaticVoid2Event(null, e);
			}
		}

		public static event VoidHandler5 StaticVoid5Event;

		public static void OnStaticVoid5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(StaticVoid5Event != null)
			{
				StaticVoid5Event(null, arg2, arg3, arg4, arg5);
			}
		}

		public static event StringHandler StaticStringEvent;

		public static void OnStaticStringEvent()
		{
			if(StaticStringEvent != null)
			{
				StaticStringEvent();
			}
		}

		public static event StringHandler2 StaticString2Event;

		public static void OnStaticString2Event(EventArgs e)
		{
			if(StaticString2Event != null)
			{
				StaticString2Event(null, e);
			}
		}

		public static event StringHandler5 StaticString5Event;

		public static void OnStaticString5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(StaticString5Event != null)
			{
				StaticString5Event(null, arg2, arg3, arg4, arg5);
			}
		}

		public static event DoubleHandler StaticDoubleEvent;

		public static void OnStaticDoubleEvent()
		{
			if(StaticDoubleEvent != null)
			{
				StaticDoubleEvent();
			}
		}

		public static event DoubleHandler2 StaticDouble2Event;

		public static void OnStaticDouble2Event(EventArgs e)
		{
			if(StaticDouble2Event != null)
			{
				StaticDouble2Event(null, e);
			}
		}

		public static event DoubleHandler5 StaticDouble5Event;

		public static void OnStaticDouble5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(StaticDouble5Event != null)
			{
				StaticDouble5Event(null, arg2, arg3, arg4, arg5);
			}
		}

		public static event PointFHandler StaticPointFEvent;

		public static PointF OnStaticPointFEvent()
		{
			if(StaticPointFEvent != null)
			{
				StaticPointFEvent();
			}
			return new PointF(float.NaN, float.NaN);
		}

		public static event PointFHandler2 StaticPointF2Event;

		public static PointF OnStaticPointF2Event(EventArgs e)
		{
			if(StaticPointF2Event != null)
			{
				StaticPointF2Event(null, e);
			}
			return new PointF(float.NaN, float.NaN);
		}

		public static event PointFHandler5 StaticPointF5Event;

		public static PointF OnStaticPointF5Event(string arg2, int arg3, DateTime arg4, float arg5)
		{
			if(StaticPointF5Event != null)
			{
				StaticPointF5Event(null, arg2, arg3, arg4, arg5);
			}
			return new PointF(float.NaN, float.NaN);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
I'm a system administrator from Moscow, Russia. Programming is one of my hobbies. I presume I'm one of the first Russians who created a Web site dedicated to .Net known that time as NGWS. However, the Web page has been abandoned a long ago.

Comments and Discussions