More… (Usage)

As mentioned earlier, MemClean! cleaning agents search for dangling references in places such as:
  1. Component event handler lists
    1. Forms, UserControls, etc
  2. Component references
  3. Delegate invocation lists
    1. events, custom delegates, etc
  4. All styles of Collections
    1. Arrays, Collections, Dictionaries, Lists, etc
  5. Singleton instances
    1. Non-static instances hidden behind a static facade

Primary Scenario: Form Disposal

The most common scenario in which to leverage MemClean! is in cleaning dangling references to a
Form being disposed. To achieve this, MemClean! cleaning agents are added to the overridden
Dispose method hidden in the <Form>.Designer.<language> file (i.e. SomeForm.Designer.cs for C#).

The example below highlights how to use all three of the cleaning agents provided by MemClean! Note
that the cleaning agents must be called before disposing of components owned by the form and before
the base class is disposed. This is necessary of course to ensure the specific form instance being cleaned
is still available for cleaning!

partial class SomeForm
{
...

protected override void Dispose(bool disposing)
{
    MemClean.Statics.Clean(typeof(SomeClass), this);
    MemClean.Forms.Clean(this, disposing);
    MemClean.Products.Clean("SomeCompanyName", "LeakOwningClass");

    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

...
}

Statics

In this example, the Statics cleaning agent is used to clean SomeForm references from all the static
patterns within SomeClass that may harbor dangling references. The agent is provided with the type
of SomeClass and a reference to a specific instance of SomeForm. This agent cleans references from
static component references, static events and delegates, static collections and singleton instances.

While the Statics agent is commonly used to clean dangling Form references from static classes
(Modules in VB.NET) as in this example, it can also be used to clean dangling references to any .NET
object instance. The following code snippet shows this powerful usage scenario.

SomeOtherClass instance = new SomeOtherClass();
MemClean.Statics.Clean(typeof(SomeClass), instance);

Forms

The example also shows the usage of the Forms cleaning agent. This powerful agent seeks and cleans
all references to the specific SomeForm instance from the entire Form hierarchy in which this instance
of SomeForm sits. Both down into all child components and up into all parent components. Only in this
way can SomeForm be truly disconnected from the .NET Form framework.

While it is true the .NET disposal pattern should handle this for you (and in general does a good job at
it), it can only release what has been designated by the developer writing the Dispose method. If the
developer has forgotten to add the appropriate code to release something to the Dispose method or
worse yet hasn’t provided for the Dispose pattern when it was needed, .NET knows nothing about the
problem.

Products

Unlike the other cleaning agents, the last and perhaps most powerful cleaning agent, Products, works
in a more generalized fashion. This agent is focused by finding all the places where LeakOwningClass
is defined in the SomeCompanyName assembly hierarchy. Upon discovering each definition of the
LeakOwningClass type, MemClean! applies the Statics cleaning agent to flush all the static
patterns within LeakOwningClass.

Since the Products cleaning agent doesn’t require a reference to a target, it can be applied in the
Dispose method of the base class in your own Form hierarchy.

Warning
The Products cleaning agent can be used in a non-targeted manor, without specifying type names.
In general, this approach should be avoided at all costs as it will most likely over clean the product. It
is only meant to be used in very specific controlled circumstances where other options have failed.

Due Diligence

Obviously, cleaning references in this manor may produce undesirable side-effects, particularly when
using the Products cleaning agent. MemClean! attempts to minimize unwanted side-effects as much
as possible by using a targeted approach. This ensures only the target in question is examined.
Everything else is left untouched.

Please test any application of MemClean! thoroughly before deployment. Remember that you are a
professional after all and that any self respecting software engineer knows to give every piece of code a
thorough trashing before releasing it into the wild. This is the least respect you can afford your clientele.

SomeLeakyThing

The MemClean! download contains a sample application, SomeLeakyThing (in both VB.NET & C#),
highlighting the common leak patterns and how to apply MemClean! cleaning agents to remedy these
leaks. The sample also reveals a nefarious leak in the beloved MS ToolStrip control, which remains
unfixed to this day, and shows how the MemClean! Forms cleaning agent remedies the problem.

Last edited Sep 30, 2009 at 3:43 AM by theMont, version 3

Comments

No comments yet.