1

In the constructor of my custom composite (inherited from SWT Composite), I register myself as an EventHandler to the Eclipse IEventBroker.

To unsubscribe upon disposal, I had overridden the dispose method where I unsubscribe myself.

But I now noticed that this unsubscription is not happening, indeed the dispose method is never getting called.

What is the correct way to unsubscribe myself, or to avoid that a closed Composite/ViewPart leaves "leaking" event handlers behind?

I use Eclipse RCP 3.x (so no annotations/injection). In E4 I could use the automatic Event subscription or the @Predestroy method, if I understood it correctly.

jhyot
  • 3,733
  • 1
  • 27
  • 44

1 Answers1

3

Use the addDisposeListener of the Composite to add a DisposeListener and do the unsubscribe in the listener.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thanks! Would it be correct to call `addDisposeListener` from the constructor of my `Composite`, just after subscribing to the `EventBroker`? – jhyot Jun 03 '15 at 16:57
  • Another question: is using the DisposeListener the "canonical" way to handle the disposing? I.e. I never have to override the dispose method? – jhyot Jun 03 '15 at 17:00
  • 1
    Yes. The dispose method is only used to start of the dispose process and is never called for children of the top level control being disposed (an internal `release` method is called instead). – greg-449 Jun 03 '15 at 17:04
  • I see. Thanks a lot for the help. I just also found this very old [article](http://www.eclipse.org/articles/Article-Writing%20Your%20Own%20Widget/Writing%20Your%20Own%20Widget.htm) which seems to say the same thing. – jhyot Jun 03 '15 at 17:11