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 624709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:13:48+00:00 2026-05-13T19:13:48+00:00

Is it necessary to set to Nothing (in Dispose()) all WithEvents fields? Apparently Handles

  • 0

Is it necessary to set to Nothing(in Dispose()) all WithEvents fields?

Apparently Handles keyword adds handlers to such fields, but does not remove it until this field is not Nothing, and this can generate memory leaks?!.

This should be specially actual on cases like

class Foo
{
    Private WithEvents _bar as Bar

    Public Sub New(ByVal bar as Bar)
        _bar = bar
    End Sub

    Private Sub Bar_Changed(ByVal sender as Object, _ 
        ByVal e as EventArgs) Handles _bar.Changed
    '...  '
    End Sub
}
  • 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-13T19:13:49+00:00Added an answer on May 13, 2026 at 7:13 pm

    This can generate memory leaks if the object to which you are subscribed lives longer than the subscriber. In the majority of cases this is not true.

    Take WinForms for instance. Typically you see the WithEvents modifier on controls in a WinForm application. The outer Form class subscribes and reacts to these events. This does not cause a memory leak though because the item to which you are subscribed, the Control instances, have roughly the same lifetime as the subscriber, the Form.

    In the case where the lifetimes do differ then yes, setting the field to Nothing will cause you to unsubscribe from the event and prevent a possible memory leak.

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

Sidebar

Related Questions

Is it necessary to set large objects to null when implementing a Dispose() method?
Is it necessary to use Nothing keyword to initialize a DataSet in VB.NET?
Possible Duplicate: Any sense to set obj = null(Nothing) in Dispose()? I understand if
By generate, I mean auto-generation of the code necessary for a particular selected (set
The question gives all necessary data: what is an efficient algorithm to generate a
I always read that it is recommended to set objects to nothing, once I
How do you set a field to DBNull in Entity Framework? The fields are
Is it necessary or advantageous to write custom connection pooling code when developing applications
Are CDATA tags ever necessary in script tags and if so when? In other
Is it really necessary to release COM components from Office PIA, when you don't

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.