0
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). I thought memory leak not happened because I already created panel and just changed panel property(visible).

user3773632
  • 445
  • 6
  • 20
  • 1
    Possible duplicate of [why does right-clicking create an orange dot in the center of the circle?](http://stackoverflow.com/questions/12692851/why-does-right-clicking-create-an-orange-dot-in-the-center-of-the-circle) – Peter Badida Jul 04 '16 at 08:05
  • 2
    Why do you think there is a memory leak? – Chris Mar 06 '17 at 12:12
  • I just opened task management in windows and made some changed combobox event and my program memory is increased! – user3773632 Mar 06 '17 at 12:14
  • This may give some clue http://stackoverflow.com/questions/2820807/windows-handle-if-a-control-is-set-to-visible-false-net – techBeginner Mar 06 '17 at 12:15
  • You are just talking about increased memory usage, not a memory leak. A memory leak is when memory is not correctly released when it should be leading the program to use memory that it can never release. What you are describing is just something using more memory which could be for any number of reasons. What you are describing doesn't sound like a problem to me - if you think it is you'll have to describe in more detail so we understand the exact problem you are seeking a solution to. – Chris Mar 06 '17 at 12:19

1 Answers1

1

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. Because they actually wrap a native Windows control, they claim a control handle from Windows. This handle is released when calling the Dispose method of the control. 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). 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.

Disposing a Control will also dispose all child controls that are in its Controls collection. Since you add both panels to that collection, you are probably safe there, too.

Sefe
  • 13,731
  • 5
  • 42
  • 55