在MVVM中,在ViewModel中挂起模型项的事件是否导致内存泄漏? [英] In MVVM, does hooking a model item's event in the ViewModel cause a memory leak?

查看:510
本文介绍了在MVVM中,在ViewModel中挂起模型项的事件是否导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果您在其相关联的ViewModel中收看ModelItem事件,请尝试跟踪我们的WPF / MVVM应用程序中的某些内存泄漏,一个实例特定的处理程序不会使ModelModelItem挂起,只要ModelItem仍然存在?



考虑这种情况...

  public class ItemViewModel 
{
public ItemViewModel(ModelItem item)
{
this.Item = item ;
item.SomeEvent + = ItemSomeEventHandler
}

Public ModelItem Item {get;私人集合

//注意:这是这个实例上的处理程序,而不是一个共享处理程序
Private void ItemSomeEventHandler(object s,EventArgs e)
{
控制台。 WriteLine(模型改变了!);
}
}

如果这确实造成泄漏,那么/在哪里你解开了吗?您不能在Dispose中执行此操作,因为如果某些东西仍然有引用,则它将不会被调用,它看起来像它一样:模型项。



将适当的地方实施Dispose在使用此ViewModel的控件中,然后在那里滴下一个事件 - 未挂钩的例程?似乎有点奸诈地依靠外部的东西,以确保你清理自己的内部,但是没有去一个EventManager架构,我不知道该怎么做。

解决方案

是的,会导致泄漏..或者更具体地说,当视图不再显示时,不会收集 ItemViewModel 因为 ModelItem 持有对 ItemViewModel



的引用如果您可以控制 ViewModel 的生命周期,您可以实现 Dispose 并取消挂起完全有效的事件处理程序。问题是什么组件创建 ItemViewModel ?如果是另一个视图模型,那么您可以将处理委托给该视图,或者如果视图创建视图模型,则可以调用 Dispose 方法。



但是,如果您使用像 MVVMLight 工具包那么您可以使用它的 ICleanup 接口或类似的东西。检查这个其他回答了解更多信息。


Trying to track down some memory leaks in our WPF/MVVM app and something occurred to me...

If you listen to ModelItem events in its associated ViewModel with an instance-specific handler, doesn’t that make the ViewModelItem hang around as long as the ModelItem still exists?

Consider this case...

public class ItemViewModel
{
    public ItemViewModel(ModelItem item)
    {
        this.Item = item;
        item.SomeEvent += ItemSomeEventHandler
    }

    Public ModelItem Item{ get; private set; }

    // Note: This is a handler on this instance, not a shared handler
    Private void ItemSomeEventHandler(object s, EventArgs e)
    {
        Console.WriteLine("The model changed!");
    }
}

If this does cause a leak, how/where do you unhook it? You can't do it in 'Dispose' since that won't be called if something still has a reference to it, which it looks like it does: the model item.

Would the proper place be to implement Dispose in the control where this ViewModel is used, then trickle down an event-unhook routine there? Seems a bit treacherous to rely on something external to make sure you clean up your own internal, but short of going to an EventManager architecture, I'm not sure what to do here.

解决方案

Yes it will cause a leak.. or more specifically the ItemViewModel will not be collected when the view is no longer displayed since the ModelItem holds a reference to the ItemViewModel

You could implement Dispose and unhook the eventhandler which is perfectly valid if you can control the lifecycle of the ViewModel. The question is what component is creating the ItemViewModel? If it is another view model then you can delegate the dispose to that or if the view creates the view model then you can have the call the Dispose method.

However if you use like the MVVMLight toolkit then you can use its ICleanup interface instead or something similar. Check this other answer out for more info.

这篇关于在MVVM中,在ViewModel中挂起模型项的事件是否导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆