Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定列表内存泄漏_C#_Memory Leaks_Clr_Inotifypropertychanged_Bindinglist - Fatal编程技术网

C# 绑定列表内存泄漏

C# 绑定列表内存泄漏,c#,memory-leaks,clr,inotifypropertychanged,bindinglist,C#,Memory Leaks,Clr,Inotifypropertychanged,Bindinglist,我的应用程序使用从Bindinglist继承的自定义列表,该列表随后绑定到所有UI控件。该列表是实现INotifyPropertyChanged的baseobjects的集合。我怀疑内存泄漏,并使用 memprofiler,它确认了我的所有列表从未被处理过,并且由于订阅了bindinglist的propertyChanged eventhandlers,所以它们仍然存在 这是我的物品样本 public abstract class BaseObject:IDataErrorInfo,INotif

我的应用程序使用从Bindinglist继承的自定义列表,该列表随后绑定到所有UI控件。该列表是实现INotifyPropertyChanged的baseobjects的集合。我怀疑内存泄漏,并使用 memprofiler,它确认了我的所有列表从未被处理过,并且由于订阅了bindinglist的propertyChanged eventhandlers,所以它们仍然存在

这是我的物品样本

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);
     }
}
公共抽象类BaseObject:IDataErrorInfo、INotifyPropertyChanged、iClonable
{
私有Guid _Id=Guid.NewGuid();
公共虚拟Guid ID
{
获取{返回此。\u Id;}
设置{this.\u Id=value;this.OnPropertyChange(新)
PropertyChangedEventArgs(propertyName));}
}
[字段:非序列化]
公共虚拟事件属性ChangedEventHandler属性Changed;
PropertyChange上受保护的虚拟无效(PropertyChangedEventArgs e)
{
如果(this.PropertyChanged!=null)this.PropertyChanged(this,e);
}   
}
[可序列化]
公共类BaseList:BindingList,ICloneable其中T:BaseObject
{
新增公共作废(T项)
{      
基础。添加(项目);
}
公共新作废删除(T项)
{
底座。移除(项目);
}
}
这是来自探查器的分配堆栈

[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
[跳过的帧]
mscorlib!System.MulticastDelegate.CombineImpl(委托)
mscorlib!System.Delegate.Combine(委托,委托)
.数据!。Data.BaseObject.add_PropertyChanged(
房地产商(开发商)
[跳过的帧]
系统!System.ComponentModel.BindingList.InsertItem(int,T)
mscorlib!System.Collections.ObjectModel.Collection.Add(T)
.Data.BaseList.Add(T)BaseList.cs
.UI.Forms.Form1.GetData()Form1
.UI.Forms.Form1.Initialize()Form1
.UI.Forms.Form1.RunAsyncComplete()Form1
有人能帮我处理一下名单吗

谢谢大家



亨克,你是对的。问题不在于我提到的代码,而是我把它缩小到了代理,这些代理阻止我的表单被处理。我正在制作一个样本来复制我将上传到该网站的问题。谢谢

我不认为问题出在这里显示的代码中,BaseObject中的事件只会导致传出引用(指向订阅者)。隐藏Add/Remove和BaseList类有点不确定。是否会干扰对PropertyChanged事件的订阅/取消订阅

where T:BaseList
是一个输入错误(我希望这里是baseObject)还是涉及到另一个类

顺便说一句,我对中国的“虚拟”有点困惑

public virtual event PropertyChangedEventHandler PropertyChanged;
我不确定你是否有这样的目的,我认为应该取消

BaseBusinessList可能应该实现IRaiseItemChangedEvents