VTK leaks while testing

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

        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()


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

3: Test checkAttributes_positive passed!
3: **************************************
3: ok
3: runTest (slicer.ScriptedLoadableModule.ScriptedLoadableModuleTest) ... No test is defined in ScriptedLoadableModuleTest
3: ok
3: ----------------------------------------------------------------------
3: Ran 2 tests in 0.732s
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/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??

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

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

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

    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')
        for sliceCompositeNode in sliceCompositeNodes:

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.

1 Like