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
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.