Other weak objects
QWeakReferenceCollection
A commonly used technique to keep track of object instances is using a static collection to register these instances. For instance, in the Qios.DevSuite library each control has its own QColorScheme that the developer can change independently from other controls. There is also a QGlobalColorScheme that provides a way to change colors globally. Since the QColorSchemes do not change when the QGlobalColorScheme changes, all QColorSchemes are registered to the QGlobalColorScheme upon creation and a method of the QColorSchemes is called when the QGlobalColorScheme changes.

The problem with such a collection is that it keeps the collection members from being disposed (imagine a large control not being disposed because of one QColorScheme reference to the QGlobalColorScheme). We use a QWeakReferenceCollection to keep track of all QColorSchemes. All objects added to the QWeakReferenceCollection are wrapped in a QWeakReference and the QWeakReferenceCollection removes any disposed objects when iterating through the collection.

Show code snippet - Using a QWeakReferenceCollection
//A QWeakReferenceCollection containing QWeakReferences to QColorSchemes
public class QColorSchemeCollection : QWeakReferenceCollection
{
	//Default constructor
	public QColorSchemeCollection()
	{}

	//Adds a QWeakReference to the QColorScheme to the collection
	public void AddColorScheme(QColorScheme colorScheme)
	{
		this.AddObject(colorScheme);
	}

	//Removes the QWeakReference of the QColorScheme from the collection
	public void RemoveColorScheme(QColorScheme colorScheme)
	{
		this.RemoveObject(colorScheme, true);
	}

	//Raises the ColorsChanged event of 
	//all QColorSchemes in the collection
	public void RaiseColorsChanged()
	{
		//iterate backwards because we remove
		//collected weak references
		QColorScheme tmp_oColorScheme;
		for (int i = this.Count - 1; i >= 0; i--)
		{
			//get the weak reference
			QWeakReference tmp_oReference = this[i];
			if ((tmp_oReference != null) &&
			    (!tmp_oReference.Finalized) &&
			    (tmp_oReference.IsAlive))
			{
				//raise the event if the
				//reference is still alive
				tmp_oColorScheme = (QColorScheme)
					tmp_oReference.Target
				tmp_oColorScheme.RaiseColorsChanged();
			}
			else
			{
				//remove the item if the
				//colorscheme is collected
				this.RemoveAt(i);
			}
		}
	}
}
	

QWeakMessageFilter
By using the IMessageFilter interface you can capture application messages before they are dispatched to the form or control. The application contains a list of all messages filters and that reference keeps the IMessageFilter from being disposed.

The QWeakMessageFilter is an intermediate class to make sure the actual IMessageFilter gets disposed when neccessary. The QWeakMessageFilter contains a QWeakReference to the actual message filter and only passes the message through if the QWeakReference has not yet been collected.

To implement a QWeakMessageFilter replace the following code:
Application.AddMessageFilter(m_oMyMessageFilter); with:
Application.AddMessageFilter(new QWeakMessageFilter(m_oMyMessageFilter));
Note: When calling the Application.AddMessageFilter to add a new IMessageFilter, the IMessageFilter is stored in the context of the current thread. The message filter cannot be removed from a different thread than the thread it was added from, therefore you cannot remove it via the finalizer. This is because the finalizer is called by the Garbage Collector and this happens in a different thread than the UI thread.
Qios.DevSuite.MemoryPack
Updated Version: 2006-09-07
Memory Management
Memory management
Introduction about automatic memory management and its problems.
More information
Publishing and consuming
Explains how to publish and consume events using the QWeakEvent structure to prevent controls from being ignored by the garbage collector.
More information
QWeakMessageFilter and
QWeakReferenceCollection
Shows how to create a collection of weak references and a weak IMessageFilter implementation.
More information
Other notes and links
A few other notes and links about memory management
More information
Qios.DevSuite.MemoryPack
Screenshots
Some MemoryPack screenshots that illustrate the difference when using our technique
More information
Download
Qios.DevSuite.MemoryPack
Download the free and opensource library & sample application that shows the difference between normal event handling and using the QWeakEvent structure.
Proceed to the download