2009-07-05 56 views
1

我的應用程序使用一個自定義的List,它繼承自Bindinglist,然後綁定到所有的UI控件。該列表是實現INotifyPropertyChanged的baseobjects的集合。我懷疑有內存泄漏,並使用 memprofiler對我的應用程序進行了描述,它確認我的所有列表從不處理,並且由於它們訂閱了綁定列表的propertyChanged事件處理程序,所以它們正在執行。BindingList內存泄漏

這裏是我的對象

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 

有人可以幫助我在得到安置名單。

感謝所有


亨克,你是對的。問題不在於我提到的代碼,而是我縮小了代碼,這些代表阻止了我的表單被處置。我正在處理一個示例以複製我將上傳到網站的問題。謝謝

回答

2

我不認爲問題出現在這裏顯示的代碼中,BaseObject中的事件只會導致傳出引用(給訂閱者)。它隱藏了Add/Remove和BaseList類會有一點點變化。難道是<自定義代碼>干擾訂閱/取消訂閱PropertyChanged事件?

而且是where T:BaseList一個錯字(我期望baseObject在這裏)還是有另一個類涉及?

在一個側面說明,我有點被「虛擬」疑惑中

public virtual event PropertyChangedEventHandler PropertyChanged; 

我不知道,如果你有一個目的,我覺得應該去。

而BaseBusinessList應該可能實施IRaiseItemChangedEvents