title: Mem leak in pyqt test with pytest
body: <p>Does anyone have an explanation for why the following leaks memory (the memory and other kernel objects like GDI and User handles keep increasing at every iteration and never go back down until test exits):</p>
  
  <pre><code>import pytest
  from PyQt5.QtCore import QTimer
  from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView
  
  class TestCase:
      @pytest.mark.parametrize('dummy', range(1000))
      def test_empty(self, dummy):
          # self.view = None   # does NOT fix the leak if uncommented!
          self.app = QApplication.instance()
          if self.app is None:
              self.app = QApplication([])
          self.view = QGraphicsView()
          self.view.setFixedSize(600, 400)
          self.view.setScene(QGraphicsScene())
          self.view.show()
  
          QTimer.singleShot(100, self.app.exit)
          self.app.exec()
  
          # self.view = None   # FIXES the leak if uncommented!
  </code></pre>
  
  <p>There is no leak if any of the following conditions becomes True: </p>
  
  <ol>
  <li>If I None-ify view <em>before</em> test method returns (uncomment the last line)</li>
  <li>If I make the view local to the
  function instead of a member of self (not surprising given fix #1)</li>
  <li>If I remove the decorator and
  instead have a "while True" at the top of the function (so the test
  itself runs once but the window gets recreated over and over)</li>
  </ol>
  
  <p>Interestingly, the leak does NOT go away if I make any of the following modifications:</p>
  
  <ol>
  <li>I set the view to None at the beginning of the function instead of at the end (commented out line at beginning of test method)</li>
  <li>Instead of parameterizing the test method, I create many test methods (100's, easily done with a little python script that generates the test module), or many test classes, many test modules (that's how I noticed the problem, we have a huge test suite consisting of 100 test modules with several classes in each, each having many test methods -- the memory leak in the test suite went unnoticed until recently when the number of tests became large enough that the OS is now running out of GDI handles before pytest finishes running all tests!).</li>
  <li>I replace the single-shot call to app.exit() by app.closeAllWindows() (I thought this might have been the issue in this MCVE)</li>
  </ol>
  
  <p>The actual tests in our app require that some objects be created in the setup_method(), hence we cannot avoid assigning PyQt objects to data members of the test instance. So the only practical solution for us now is to edit each test method to None-ify PyQt objects created by the methods but this will be error prone, not to mention laborious (although better than current situation). I'm hoping there is a better way. </p>