内存泄漏的ContextMenuStrip [英] Memory leak with ContextMenuStrip

查看:137
本文介绍了内存泄漏的ContextMenuStrip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了很多自定义控件并将其添加到一个FlowLayoutPanel的。还有创建和填充在设计时的ContextMenuStrip。

I'm creating a lot of custom controls and adding them to a FlowLayoutPanel. There is also a ContextMenuStrip created and populated at design time.

每一个控件添加到它分配给该菜单的ContextMenuStrip属性,专案组时间,使所有的控制共享同一个菜单。但我注意到,当控件从面板上拆除和处置,在任务管理器中使用的内存不下降。它每次创建控件并添加到布局面板。时间上升50KB左右。

Every time a control is added to the panel it has its ContextMenuStrip property assigned to this menu, so that all controls "share" the same menu. But I noticed when the controls are removed from the panel and disposed of, the memory in use in Task Manager doesn't drop. It rises around 50kB every time a control is created and added to the layout panel.

我下载了.NET内存分析器的审判,它显示有提到菜单条挂在对照组处理后。我改变了code到处置的控制之前,明确ContextMenuStrip属性为null设置,并且是的,内存,现予以公布。为什么是这样?如果不是GC清理这种类型的东西?

I downloaded the trial of .NET Memory Profiler and it showed there were references to the menu strip hanging around after the controls were disposed. I changed the code to explicitly set the ContextMenuStrip property to null before disposing of the control, and yep, the memory is now released. Why is this? Shouldn't the GC clean up this type of thing?

推荐答案

如果你看一看控制的ContexmenuStrip属性,你会看到二传手订阅控制到MenuStrip中的处置事件,创造一个背从MenuStrip中提及的控制。

If you take a look at the ContexmenuStrip property of Control, you will see that the setter subscribes the control to the Disposed event of the MenuStrip, creating a back-reference from the MenuStrip to the Control.

这意味着它是一个经典案例可达通事件,而你已经找到了解决办法:设置ContexmenuStrip属性设置为null。

This means it is a classic case of reachable-through-event and you already found the solution: set the ContexmenuStrip property to null.

这篇关于内存泄漏的ContextMenuStrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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