views:

473

answers:

2

Hi All

My application uses a custom List that inherits from Bindinglist which is then bound to all the UI controls. The list is a collection of baseobjects which implements INotifyPropertyChanged. I suspected a memory leak and profiled my application using memprofiler which confirmed that that all my lists are never disposed and that they are clinging on because they are subscribed to the propertyChanged eventhandlers of the bindinglist.

Here is the sample of my objects

public abstract class BaseObject:IDataErrorInfo,INotifyPropertyChanged,ICloneable
{
private Guid _Id = Guid.NewGuid();
public virtual Guid ID
{
 get { return this._Id; }
 set { this._Id = value;  this.OnPropertyChange(new 
                           PropertyChangedEventArgs(propertyName)); }
}

[field:NonSerialized]
public virtual event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChange(PropertyChangedEventArgs e)
{
 if (this.PropertyChanged != null) this.PropertyChanged(this, e);
} 
}

[Serializable]
public class BaseList<T> :BindingList<T>,ICloneable where T:BaseObject
{
public new void Add(T item)
{    
 <Custom code>
 base.Add(item);
     }

public new void Remove(T item)
{
 <Custom Code>
 base.Remove(item);
     }
}

Here is the allocation stack from the profiler

[Skipped frame(s)]
mscorlib!System.MulticastDelegate.CombineImpl( Delegate )
mscorlib!System.Delegate.Combine( Delegate, Delegate )
<AppName>.Data!<AppName>.Data.BaseObject.add_PropertyChanged( 
              PropertyChangedEventHandler )

[Skipped frame(s)]
System!System.ComponentModel.BindingList<T>.InsertItem( int, T )
mscorlib!System.Collections.ObjectModel.Collection<T>.Add( T )
<AppName>.Data.BaseList<T>.Add( T ) BaseList.cs
<AppName>.UI.Forms.Form1.GetData() Form1
<AppName>.UI.Forms.Form1.Initialize() Form1
<AppName>.UI.Forms.Form1.RunAsyncComplete() Form1

Can someone help me in getting the lists disposed.

Thanks all

A: 

Henk, you were right. The problem was not with the code I mentioned, instead I have narrowed it down to delegates which are preventing my form from being disposed. I am working on a sample to replicate the problem which I will upload to the site. Thanks

Raj G
A: 

Did you get any solution to the problem?

Manoj
It should be a comment, not an answer. Please delete your answer and post a comment instead.
abatishchev