# VTK leaks while testing

**URL:** https://discourse.slicer.org/t/vtk-leaks-while-testing/9026
**Category:** Development
**Tags:** vtk
**Created:** [November 5, 2019, 8:57am UTC](https://discourse.slicer.org/t/vtk-leaks-while-testing/9026 "2019-11-05T08:57:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)
#### Post date: [November 5, 2019, 8:57am UTC](https://discourse.slicer.org/t/vtk-leaks-while-testing/9026/1 "2019-11-05T08:57:07Z")

</div>

Basically I have this code for setting a default volume in the views

```python
        mainWindow = slicer.util.mainWindow()
        if mainWindow is not None:
            layoutManager = slicer.app.layoutManager()
            if layoutManager is not None:
                
                sliceLogic = layoutManager.sliceWidget('Red').sliceLogic()
                CompositeNode = sliceLogic.GetSliceCompositeNode()
                CompositeNode.SetLinkedControl(1)
                sliceLogic.StartSliceCompositeNodeInteraction(1)
                CompositeNode.SetBackgroundVolumeID(minCTID)
                CompositeNode.SetForegroundVolumeID(minSPECTID)
                sliceLogic.EndSliceCompositeNodeInteraction()

                layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpView)
                slicer.util.resetSliceViews()

```

It works like a charm in normal mode, but when testing with `--no-main-window` it creates the following error

```bash
3: Test checkAttributes_positive passed!
3: 
3: **************************************
3: 
3: ok
3: runTest (slicer.ScriptedLoadableModule.ScriptedLoadableModuleTest) ... No test is defined in ScriptedLoadableModuleTest
3: ok
3: 
3: ----------------------------------------------------------------------
3: Ran 2 tests in 0.732s
3: 
3: OK
3: vtkDebugLeaks has detected LEAKS!
3: Class "vtkObserverManager" has 1 instance still around.
3: Class "vtkMRMLSliceCompositeNode" has 1 instance still around.
3: Class "vtkCommand or subclass" has 2 instances still around.
3: 
3/4 Test #3: py_Dosimetry4DTest ...............***Failed 10.47 sec

```

As you can see the test passed, but the VTK leaks breaks the workflow  
Without the above code, the VTK leaks are not present. However that code is not intended to run at all in no window mode, that is why I check if there is a main window. However, the check is failing as it creates the composite nodes. How is this possible?? Is there a way to NOT run a piece of code in no window mode??

---

<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: [November 5, 2019, 2:02pm UTC](https://discourse.slicer.org/t/vtk-leaks-while-testing/9026/2 "2019-11-05T14:02:59Z")

</div>

Have you included the complete test here? If there is no main window then the script above should not do anything.

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [November 5, 2019, 2:44pm UTC](https://discourse.slicer.org/t/vtk-leaks-while-testing/9026/3 "2019-11-05T14:44:02Z")

</div>

I get the exact vtkDebugLeaks message only if I have implemented code in python such as `slicer.mrmlScene.CreateNodeByClass("vtkMRMLSliceCompositeNode")`.

See [https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MemoryManagement#Python\_scripts\_and\_scripted\_modules](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MemoryManagement#Python_scripts_and_scripted_modules)

---

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)
#### Post date: [November 5, 2019, 2:48pm UTC](https://discourse.slicer.org/t/vtk-leaks-while-testing/9026/4 "2019-11-05T14:48:21Z")

</div>

@lassoan I found out that I was doing in the test

```python
    def makeSlicerLinkedCompositeNodes():
        # Set linked slice views in all existing slice composite nodes and in the default node
        sliceCompositeNodes = slicer.util.getNodesByClass('vtkMRMLSliceCompositeNode')
        defaultSliceCompositeNode = slicer.mrmlScene.GetDefaultNodeByClass('vtkMRMLSliceCompositeNode')
        if not defaultSliceCompositeNode:
            defaultSliceCompositeNode = slicer.mrmlScene.CreateNodeByClass('vtkMRMLSliceCompositeNode')
            slicer.mrmlScene.AddDefaultNode(defaultSliceCompositeNode)
        sliceCompositeNodes.append(defaultSliceCompositeNode)
        for sliceCompositeNode in sliceCompositeNodes:
            sliceCompositeNode.SetLinkedControl(True)

```

without checking properly if there was a Slicer window, this was the problem. So I changed the function to check for mainwindow first. Sorry for bothering you.
