Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7758301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:19:26+00:00 2026-06-01T13:19:26+00:00

I am going to make a GUI that will have dynamically created sets of

  • 0

I am going to make a GUI that will have dynamically created sets of controls with events assigned to them. I will need to add and remove those controls at runtime. It will look like this:

FlowLayoutPanel.Controls.Clear();
<< add new controls, assigning Click events with += >>

I have heard that assigning event handlers with += can cause memory leaks (more specificly, memory will not be freed until application has exited). I want to avoid this. I know i can write some functions like here How to remove all event handlers from a control to find all event handlers and remove them but it looks very complex.

Is there another way? Does calling Dispose help remove those event handlers? Can you destroy objects to force their memory to be freed like in C/C++?

Thanks!

PS: Problem is, i dont know what event to detach. I will create lots of labels and add different kinds of onclick events to them. When its time to clean the flow layout panel, there is no way to know what event handler was attached to which label.

This is the example code (_flowLP is a FlowLayoutPanel) – this Refresh() function is ran multiple times before the application exits.

    private void Refresh()
    {
        Label l;
        Random rnd = new Random();

        // What code should i add here to prevent memory leaks
        _flowLP.Controls.Clear();

        l = new Label();
        l.Text = "1";
        if (rnd.Next(3) == 0) l.Click += Method1;
        if (rnd.Next(3) == 0) l.Click += Method2;
        if (rnd.Next(3) == 0) l.Click += Method3;
        _flowLP.Controls.Add(l);

        l = new Label();
        l.Text = "2";
        if (rnd.Next(3) == 0) l.Click += Method1;
        if (rnd.Next(3) == 0) l.Click += Method2;
        if (rnd.Next(3) == 0) l.Click += Method3;
        _flowLP.Controls.Add(l);

        l = new Label();
        l.Text = "3";
        if (rnd.Next(3) == 0) l.Click += Method1;
        if (rnd.Next(3) == 0) l.Click += Method2;
        if (rnd.Next(3) == 0) l.Click += Method3;
        _flowLP.Controls.Add(l);

        l = new Label();
        l.Text = "4";
        if (rnd.Next(3) == 0) l.Click += Method1;
        if (rnd.Next(3) == 0) l.Click += Method2;
        if (rnd.Next(3) == 0) l.Click += Method3;
        _flowLP.Controls.Add(l);

        l = new Label();
        l.Text = "5";
        if (rnd.Next(3) == 0) l.Click += Method1;
        if (rnd.Next(3) == 0) l.Click += Method2;
        if (rnd.Next(3) == 0) l.Click += Method3;
        _flowLP.Controls.Add(l);

        l = new Label();
        l.Text = "6";
        if (rnd.Next(3) == 0) l.Click += Method1;
        if (rnd.Next(3) == 0) l.Click += Method2;
        if (rnd.Next(3) == 0) l.Click += Method3;
        _flowLP.Controls.Add(l);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:19:27+00:00Added an answer on June 1, 2026 at 1:19 pm

    This is mainly going to be a worry when you attach a shorter lived event consumer to a longer lived event producer. If they have similar lives or are the opposite of what I described, it’s a non-issue.

    In the case where you do worry about this, just use -= to detach from the event. That removes the reference created by attachment and helps avoid this type of memory issue.

    Edit: Since the comments are getting a little long, I’ll post some follow up here. When you attach to an event, what you’re doing is hanging a reference to yourself on the event provider. So, for instance, if you had a Clock class with a StrikesMidnight event and you subscribe to that event from a class called Bedtime, the actual mechanics of Bedtime saying clock.StrikesMidnight += this.HandleMidnight; is that you’re assigning clock a reference to yourself. It’s as if Clock had an object property and you said clock.ObjectProperty = this;

    So, in a case where the Bedtime class is short-lived and goes out of scope, Bedtime shows up, hangs a reference to itself on Clock, and then goes out of scope. Problem is, Clock still has a reference to it, so even though it’s out of scope, Garbage Collector won’t collect Bedtime.

    ….

    That’s the background. In your case, you’re creating a label and attaching a reference to yourself to it (via your “MethodX” handlers). When refresh is called, you clear your list of labels (meaning they go out of scope). They go out of scope and they have references to your class via its MethodX handlers, but so what? Them having references does not prevent them from being GC’ed. Nobody is holding references to them in your code, so the GC will do its work on them and you won’t leak memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going to create a utility with GUI that will run on Windows operating
I'm going to make an application (in Swing) that uses a tree to visualize
Hi guys i am going to make a android game. i wanna know that
I'm developing a system in Ruby that is going to make use of RabbitMQ
I am working on a CSS/HTML submit form that I am going to make
I'm about to have to write a web page/app that will serve the agenda
I'm trying to make a bash script that will talk to a java program
I'm writing a utility in C# that will make managing multiple Mercurial repositories easier
Im going to make a wizard in asp.net. Step one will be about 7
Im going to make simple game with fallowing architecture: Initialization OpenWindowAndGraphics while(game_not_end) { ReadEvents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.