Search code examples
javadependency-injectionswtrcpe4

e4: dependency injection and disposing widgets


I have got some instances of a class which is derived from an SWT Composite. These instances are injected with ContextInjectionFactory. An example of the class would be:

public class MyTest extends Composite {
    public MyTest(Composite parent, int style) {
        super(parent, style);
    }

    @Inject
    @Optional
    private void doSomething(@UIEventTopic(EventConstants.TEST) Object unused) {
        //do something
    }
}

And this is how I use it:

public createTestContent() {
    MyTest myTest = new MyTest(composite, SWT.NONE);
    ContextInjectionFactory.inject(myTest, context);
}

public someAction() {
    composite.dispose();
}

If I dispose off these objects now (by disposing their parent), I would expect that the method doSomething from the example code is not called anymore. But in fact it will still be called.

In order to stop this behavior I need to call

ContextInjectionFactory.uninject(myTest, context);

Does anyone know why this is happening?


Solution

  • When you use @UIEventTopic Eclipse has to remember the method so that it can call it whenever an event occurs. Eclipse continues to remember and call this method until you run ContextInjectionFactory.uninject on the class instance.

    This has nothing to do with disposing of SWT controls - that just releases any native data being used by the controls.

    It looks like the ExtendedObjectSupplier which deals with @UIEventTopic uses a WeakReference to remember the method information so the data may sometimes be disposed when memory is tight.