# Deleted widget's callback is executed

**URL:** https://discourse.vtk.org/t/deleted-widgets-callback-is-executed/8391
**Category:** Web
**Created:** [April 28, 2022, 1:42am UTC](https://discourse.vtk.org/t/deleted-widgets-callback-is-executed/8391 "2022-04-28T01:42:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![obourgart](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@obourgart](https://discourse.vtk.org/u/obourgart)
#### Post date: [April 28, 2022, 1:42am UTC](https://discourse.vtk.org/t/deleted-widgets-callback-is-executed/8391/1 "2022-04-28T01:42:47Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![Forrest](https://discourse.vtk.org/user_avatar/discourse.vtk.org/forrest/32/300_2.png) [@Forrest](https://discourse.vtk.org/u/Forrest)
#### Post date: [April 29, 2022, 4:19pm UTC](https://discourse.vtk.org/t/deleted-widgets-callback-is-executed/8391/2 "2022-04-29T16:19:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![obourgart](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@obourgart](https://discourse.vtk.org/u/obourgart)
#### Post date: [April 29, 2022, 4:33pm UTC](https://discourse.vtk.org/t/deleted-widgets-callback-is-executed/8391/3 "2022-04-29T16:33:40Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![Forrest](https://discourse.vtk.org/user_avatar/discourse.vtk.org/forrest/32/300_2.png) [@Forrest](https://discourse.vtk.org/u/Forrest)
#### Post date: [April 29, 2022, 4:52pm UTC](https://discourse.vtk.org/t/deleted-widgets-callback-is-executed/8391/4 "2022-04-29T16:52:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Forrest](https://discourse.vtk.org/user_avatar/discourse.vtk.org/forrest/32/300_2.png) [@Forrest](https://discourse.vtk.org/u/Forrest)
#### Post date: [May 17, 2022, 7:36pm UTC](https://discourse.vtk.org/t/deleted-widgets-callback-is-executed/8391/5 "2022-05-17T19:36:45Z")

</div>

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