BindingList内存泄漏
我的应用程序使用继承自 Bindinglist 的自定义列表,然后将其绑定到所有 UI 控件。 该列表是实现 INotifyPropertyChanged 的基础对象的集合。 我怀疑内存泄漏并使用以下命令分析了我的应用程序 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
有人可以帮助我处理列表吗?
谢谢所有的
亨克,你是对的。 问题不在于我提到的代码,而是我将其范围缩小到阻止我的表单被处理的代表。 我正在制作一个示例来复制问题,并将其上传到网站。 谢谢
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为问题出在此处显示的代码中,BaseObject 中的事件只会导致传出引用(对订阅者)。 由于隐藏了 Add/Remove 和 BaseList 类,它变得有点不确定。 难道是< 自定义代码> 干扰订阅/取消订阅 PropertyChanged 事件?
where T:BaseList
是一个拼写错误(我希望这里是 baseObject)还是还涉及另一个类?顺便说一句,我对“虚拟”有点困惑,
我不确定你是否有这样做的目的,我认为它应该消失。
BaseBusinessList 可能应该实现 IRaiseItemChangedEvents
I don't believe the problem is in the code shown here, the event in BaseObject only causes outgoing references (to the subscribers). It gets a bit iffy with the hiding of Add/Remove and the BaseList class. Could it be that the < Custom Code> interferes with subscribing/unsubscribing to the PropertyChanged event?
And is
where T:BaseList
a typo (I would expect baseObject here) or is there another class involved?On a side note, I am a bit puzzled by the 'virtual' in
I am not sure if you have a purpose for that, I think it should go.
And the BaseBusinessList should probably Implement IRaiseItemChangedEvents