This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

event listeners cleaned up properly?  RSS feed

 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever I create any type of GUI component, it normally has one or more event listeners attached to it. I add all of these using addXXXListener() etc.
When the component gets garbage collected, does the reference to the event listener get left somewhere, meaning that the event listener object will never get cleaned up until the program finishes? Should removeXXXListener() be used by every GUI component in the finalize() method?
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,
No, you don't normally need to worry about eliminating references to the event listeners. Once the component becomes unreachable (i.e., there are no more direct or indirect references to it from any active thread), any objects that it references (e.g., the registered listeners) become unreachable as well. The only thing that might prevent that from occurring is if you've created references to the listeners in some place other than the component's listener list, but there's rarely any reason to do that and is probably not done in your code.
------------------
Brett Spell
Author, Professional Java Programming
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!