简体   繁体   English

C#面板属性更改时内存泄漏

[英]c# there is memory leak when panel property changed

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OcppDummyClient
{
    public partial class Form1 : Form
    {
        public string[] messages = new string[]
        {
           "Authorize",
           "BootNoficiation"
        };

        public Panel A;
        public Panel B;

        public Form1()
        {
            InitializeComponent();
            InitializeForm();

            A = new Panel()
            {
                Width = this.flowLayoutPanel1.Width,
                Height = this.flowLayoutPanel1.Height,
                BackColor = Color.Black
            };

            A.Controls.Add(new Button()
            {
                Text = "Button"
            });

            B = new Panel()
            {
                Width = this.flowLayoutPanel1.Width,
                Height = this.flowLayoutPanel1.Height,
                BackColor = Color.Blue
            };

            B.Controls.Add(new Button()
            {
                Text = "Button2"
            });

            this.flowLayoutPanel1.Controls.Add(A);
            this.flowLayoutPanel1.Controls.Add(B);
        }

        public void InitializeForm()
        {
            this.comboBox1.Items.AddRange(messages);
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            string SelectedValue = this.comboBox1.Text.ToString();

            switch(SelectedValue)
            {
                case "Authorize":
                {
                    A.Visible = true;
                    B.Visible = false;
                    break;
                }

                case "BootNoficiation":
                {
                    A.Visible = false;
                    B.Visible = true;
                    break;
                }

                default:
                {
                    break;
                }
            }
        }
    }
}

That is my whole code and I want to know why memory leak is occurred when I changed combobox with event handler(comboBox1_SelectedValueChanged). 这就是我的全部代码,我想知道为什么在使用事件处理程序(comboBox1_SelectedValueChanged)更改组合框时会发生内存泄漏。 I thought memory leak not happened because I already created panel and just changed panel property(visible). 我以为没有发生内存泄漏,因为我已经创建了面板并且只是更改了面板属性(可见)。

A memory increase does not indicate a memory leak in managed memory. 内存增加并不表示托管内存中发生内存泄漏。 The memory will only be reclaimed upon garbage collection. 仅在垃圾回收时才回收内存。 Until then memory usage will increase. 在此之前,内存使用量将增加。 A memory "leak" happens when an object that should be garbage collected is not really dead (because there are some unintended active references to it). 当一个应该被垃圾回收的对象没有真正死亡时,就会发生内存“泄漏”(因为有一些意外的主动引用)。

Having said that, you should still be careful with WinForms controls. 话虽如此,您仍应谨慎使用WinForms控件。 Because they actually wrap a native Windows control, they claim a control handle from Windows. 因为它们实际上包装了本机Windows控件,所以它们要求Windows的控件句柄。 This handle is released when calling the Dispose method of the control. 调用控件的Dispose方法时,将释放此句柄。 This can (and in most cases will) happen during garbage collection, but until then the handle is not returned to windows (and there is no 100% guarantee of diposal during garbage collection). 这可以(在大多数情况下)在垃圾回收期间发生,但在此之前,句柄不会返回到Windows(并且在垃圾回收期间不能100%保证垃圾被处置)。 To avoid a resource leak from too many open Windows handles, you should dispose WinForms controls as soon as you don't need them anymore. 为避免过多的打开的Windows句柄导致资源泄漏,您应该在不再需要WinForms控件时立即对其进行处置。

Disposing a Control will also dispose all child controls that are in its Controls collection. 处置Control还将处置其Controls集合中的所有子控件。 Since you add both panels to that collection, you are probably safe there, too. 由于您将两个面板都添加到了该集合,因此在那里也可能很安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM