Eclipse RCP: How and when to correctly unsubscribe

2019-08-11 18:05发布

问题:

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.

回答1:

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