BindingList内存泄漏

时间:2009-07-05 10:55:34

标签: c# memory-leaks clr inotifypropertychanged bindinglist

我的应用程序使用从Bindinglist继承的自定义List,然后绑定到所有UI控件。该列表是实现INotifyPropertyChanged的baseobjects的集合。我怀疑是内存泄漏并使用了我的应用程序 memprofiler确认我的所有列表都没有被处理,并且他们正在坚持,因为他们订阅了绑定列表的propertyChanged事件处理程序。

以下是我的对象样本

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);
     }
}

以下是分析器

的分配堆栈
[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

有人可以帮助我处理清单。

全部谢谢


Henk,你是对的。问题不在于我提到的代码,相反,我已将其缩小到阻止我的表单被处理的代表。我正在研究一个样本来复制我将上传到网站的问题。谢谢

1 个答案:

答案 0 :(得分:2)

我不相信问题出现在这里显示的代码中,BaseObject中的事件只会导致传出引用(对订阅者)。隐藏Add / Remove和BaseList类会有点不确定。可能是&lt;自定义代码&gt;干扰订阅/取消订阅PropertyChanged事件?

并且where T:BaseList是一个拼写错误(我希望这里有baseObject)还是涉及另一个类?

另一方面,我对

中的'虚拟'感到有些困惑
public virtual event PropertyChangedEventHandler PropertyChanged;

我不确定你是否有目的,我认为应该这样做。

BaseBusinessList应该可能实现IRaiseItemChangedEvents