Deleted widget's callback is executed

I create an instance of vtkImageCroppingWidget by
const widget = vtkImageCroppingWidget.newInstance();.
I attach a callback to its cropping planes state.
After a while I delete this widget by calling widgetManager.removeWidget(widget); and widget.delete();.
At some point I create another cropping widget in another place in the system, and while interacting with it, in addition to its new callback, the old callback is also called.

The question - is it by design that all instances of the same widget class share the state and the callbacks?

Thanks!

All widgets created from the same factory will share the state. Your widget variable is actually a widget factory, and widgetManager.addWidget(widget) will create an instance of the widget for that particular renderer. This allows for multiple renderers to render the same widget state.

You will need to take the return from subscription = widgetState.onModified(...) and call subscription.unsubscribe() to remove the old callback.

Thanks for your reply!
Sorry, I haven’t made myself clear.
The widget created later was created by another widget factory:

widgetFactory1 = vtkImageCroppingWidget.newInstance();
...
widgetFactory2 = vtkImageCroppingWidget.newInstance();

Then a widget created by widgetFactory2 invokes the callback that was attached to widgetFactory1.
Seems like a bug, or I don’t understand something about the architecture.

That indeed should not be occurring. I will take a look.

1 Like

This will be resolved in this PR: fix(ImageCroppingWidget): avoid state reuse by floryst · Pull Request #2421 · Kitware/vtk-js · GitHub