# Scripted Module Leak - Addobserver

**URL:** <https://discourse.slicer.org/t/scripted-module-leak-addobserver/121>\
**Category:** Development\
**Created:** [April 14, 2017, 7:20pm UTC](https://discourse.slicer.org/t/scripted-module-leak-addobserver/121 "2017-04-14T19:20:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discourse.slicer.org/u/DanC)\
**Post date:** [April 14, 2017, 7:20pm UTC](https://discourse.slicer.org/t/scripted-module-leak-addobserver/121/1 "2017-04-14T19:20:03Z")

</div>

Hi all.  
My scripted module is giving me vtkDebugLeaks on termination of the Slicer Program.  
It happens when I add an observer.

slicer.mrmlScene.AddObserver(“ModifiedEvent”, self.sliceModifiedCallback)

Is there a way to remove the observer upon program close?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Johan\_Andruejol](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/johan_andruejol/32/3639_2.png) [@Johan\_Andruejol](https://discourse.slicer.org/u/Johan_Andruejol)\
**Post date:** [April 14, 2017, 7:35pm UTC](https://discourse.slicer.org/t/scripted-module-leak-addobserver/121/2 "2017-04-14T19:35:30Z")

</div>

Hello Dan,

You can remove an observer by using its tag. You get the tag when you add the observer [`[1]`](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_can_I_access_callData_argument_in_a_VTK_object_observer_callback_function).  
For example:  
`self.observerTag = slicer.mrmlScene.AddObserver(vtk.vtkCommand.ModifiedEvent, self.sliceModifiedCallback)`

You can then remove the observation by doing:  
`slicer.mrmlScene.RemoveObserver(self.example)`.

If you have a lot of events to observe, I would suggest that you take a look at the [`VTKObservationMixin`](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py#L690). This should make it easier for you.

Thanks !  
Johan  
[1]: [https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#How\_can\_I\_access\_callData\_argument\_in\_a\_VTK\_object\_observer\_callback\_function](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_can_I_access_callData_argument_in_a_VTK_object_observer_callback_function)  
[2]: [https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py#L690](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py#L690)

---

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discourse.slicer.org/u/DanC)\
**Post date:** [April 14, 2017, 7:42pm UTC](https://discourse.slicer.org/t/scripted-module-leak-addobserver/121/3 "2017-04-14T19:42:22Z")

</div>

Thanks for the reply Johan.  
I’ve actually seen that reference and was trying to figure out how to apply this to the module.

Is there a special method that will be called upon program close?  
Where should I place  
slicer.mrmlScene.RemoveObserver(self.observerTag )

Thanks.

---

<div class="post-metadata">

**Author:** ![Johan\_Andruejol](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/johan_andruejol/32/3639_2.png) [@Johan\_Andruejol](https://discourse.slicer.org/u/Johan_Andruejol)\
**Post date:** [April 14, 2017, 8:19pm UTC](https://discourse.slicer.org/t/scripted-module-leak-addobserver/121/4 "2017-04-14T20:19:51Z")

</div>

DanC,

You could listen to the Qt signal sent by the [`qSlicerModuleFactoryManager::moduleAboutToBeUnloaded(QString)`](https://github.com/Slicer/Slicer/blob/master/Base/QTCore/qSlicerModuleFactoryManager.h#L104). This signal should be sent when the application closes.  
You can access the `qSlicerModuleFactoryManager` using the slicer application like so:  
`slicer.app.moduleManager().factoryManager()`.

Johan

---

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discourse.slicer.org/u/DanC)\
**Post date:** [April 14, 2017, 8:42pm UTC](https://discourse.slicer.org/t/scripted-module-leak-addobserver/121/5 "2017-04-14T20:42:19Z")

</div>

Thanks.  
That solved the problem.

```
moduleManager = slicer.app.moduleManager()
moduleManager.connect( 'moduleAboutToBeUnloaded(QString)', self.removeObserver )

```

def removeObserver(self):  
self.redRenderer.RemoveObserver(self.observerTag )

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 14, 2017, 9:59pm UTC](https://discourse.slicer.org/t/scripted-module-leak-addobserver/121/6 "2017-04-14T21:59:25Z")

</div>

Most often you remove observers in the cleanup() method of the module widget class. If you want to have observers only while the module is active then add observers in enter() and remove observers in exit() method of the widget class.

You can also use [VTKObservationMixin](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py#L690), which makes adding observation simpler and automatically removes all observers when the owner class is deleted. See for example [Segment editor module](https://github.com/Slicer/Slicer/blob/master/Modules/Scripted/SegmentEditor/SegmentEditor.py) and [VTKObservationMixin tests](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/tests/test_slicer_util_VTKObservationMixin.py).
