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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:12:42+00:00 2026-06-07T08:12:42+00:00

Does using lambda expressions leed to memory leak? I’m trying to declare a delegate

  • 0

Does using lambda expressions leed to memory leak?

I’m trying to declare a delegate inside a sub and use it as a “once only” event handler then set it to nothing once it has been fired. somthing like this:

Private Sub MySub()
    Dim o As SomeObject
    Dim d as System.Delegate = Sub()
                  'Do some stuff
                   d = Nothing
                End Sub
    AddHandler o.SomeEvent, d
End Sub

but I get an invalid cast exception on the addhandler. but is something like this possible/needed?

sry for no code hilite i’m on stack mobile

  • 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-07T08:12:45+00:00Added an answer on June 7, 2026 at 8:12 am

    The following example is a windows form; the form contains a single button with its Click event wired to the subroutine: Button1_Click.

    Clicking the button causes the SomeClass to be created (o), an event handler is added and the work started. StartWork will call the Anonymous method which creates a thread, using another Anonymous method which simulates work and then releases the (o) reference.

    Public Class Form1
        Private Class SomeClass
            Public Event DoWork()
    
            Sub StartWork()
                RaiseEvent DoWork()
            End Sub
        End Class
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Debug.WriteLine("Button1_Click Started.")
            ' create class
            Dim o As New SomeClass()
            ' add handlers
            AddHandler o.DoWork, Sub()
                                     Debug.WriteLine("o.DoWork Event Handler Started.")
                                     ' threading
                                     Dim t As New Threading.Thread(Sub()
                                                                       Debug.WriteLine("Worker Thread Started.")
                                                                       ' simulate work (5 seconds)
                                                                       Threading.Thread.Sleep(5000)
                                                                       ' release reference
                                                                       o = Nothing
                                                                       Debug.WriteLine("Worker Thread Stopped.")
                                                                   End Sub)
                                     t.Start()
                                     Debug.WriteLine("o.DoWork Event Handler Stopped.")
                                 End Sub
            ' start the work
            o.StartWork()
            Debug.WriteLine("Button1_Click Stopped.")
        End Sub
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do a JOIN in Linq using lambda expressions ... and running
I'm trying to figure out how to start using linq and lambda expressions. First
Is there any overhead associated with using lambda expressions in C++0x (under VS2010)? I
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
Question: I have just wrote my first code using c# lambda expressions. It works,
This lambda does not compile, but I do not understand why. using System; using
i have a number of lambda expressions that will be using the same predicate
I'm trying to use a Lambda expression and reflection to get a member hierarchical
I'm trying to DRY out some lambda expressions for security rights. Is it possible
I'm using F# to create a lambda calculus. I am currently stuck trying to

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.