Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

event listeners cleaned up properly?

Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic