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

  • SEARCH
  • Home
  • 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 6111731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:39:52+00:00 2026-05-23T14:39:52+00:00

I know unsubscription from event IS necessary. My questions comes from the generated code:

  • 0

I know unsubscription from event IS necessary.
My questions comes from the generated code:
When you modify an ui from the VS editor, and add an event handler to a UI element (ex:
private void BtnSampleClick(object sender, EventArgs e))

When creating this event handling, VS adds this code in the private void InitializeComponent() autogenerated code

this.btnSample.Click += new System.EventHandler(this.BtnSampleClick);

Problem is that VS doesn’t add the unsubscription (this.btnSample.Click -= new System.EventHandler(this.BtnSampleClick); ) automatically in the Dispose method of the form.

Normally we should add them there right? If not this will leak to memory leaks?
Wanted to check if there was a reason why VS doesn’t perform the unsubscription automatically. Maybe the form is correctly disposed even if we don’t do it?

Thanks for helping me shred some light in this matter!

  • 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-05-23T14:39:52+00:00Added an answer on May 23, 2026 at 2:39 pm

    This isn’t done, mainly because it’s really not necessary in this case. The reason is that your Form is subscribing to events of objects which have a lifetime managed by the form. When the object (ie: the button) is unrooted from a GC perspective, the form will also be unrooted (and closed), so there is no chance of a memory leak. The GC in .NET is smart – circular references like this are not an issue.

    Unsubscribing to events is still a good general practice, however. It becomes important if you subscribe to an event on an object which has a lifetime independent of the object which is making the subscription. This is especially true if the object with the event is much longer lived than the subscriber. This case is where event-caused memory leaks tend to occur. For example, if your Form subscribes to an event on a static instance, and forgets to unsubscribe, the form will never get garbage collected, since the delegate reference will keep it “rooted” via the event subscription.

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

Sidebar

Related Questions

I know about Papervision 3D. However, alot of the realism there comes from textures.
I know, other RCS are better, but... I have moved an SVN repository from
I know there are similar questions already on SO but none of them seem
Know of a way to mock %[]? I'm writing tests for code that makes
I know Google Maps (or VLC used to) lets you share files from iTunes
I know it is possible to add new CSS classes definitions at runtime through
I know you can minify/condense Javascript or CSS code, but can you do this
Anyone know what type of JSON (if even that!) the following code is? I'm
I know that lot of questions about HTML sanitizers have appeared in SO, but
Know of an OCAML/CAML IDE? Especially one that runs on Linux?

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.