Will Dispose automaticaly unhook event handlers

D

Doug Crawford

I have a windows form where I am dynamically creating and destroying
TextBox controls. Each time I create a TextBox I also set up up a Click
event handler. If I remove the TextBox from the form's Controls
container and then call Dispose() will it automatically remove my Click
event handler and thus make the TextBox available for Garbage
collection? Or do I have to explicitly remove the Click handler myself
before calling Dispose()?
 
M

Minerva Jones

I have a windows form where I am dynamically creating and destroying
TextBox controls. Each time I create a TextBox I also set up up a Click
event handler. If I remove the TextBox from the form's Controls
container and then call Dispose() will it automatically remove my Click
event handler and thus make the TextBox available for Garbage
collection? Or do I have to explicitly remove the Click handler myself
before calling Dispose()?

AIUI, you don't have to remove the event handler. GC will be clever
enough to know that the click handler you created only has a reference
in that TextBox instance, which itself has no references. They will
both be cleaned up on the same run of the GC collection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top