title: PyQtGraph PlotWidget crashes application on closing window
body: <p>I'm attempting to build a desktop app which will chart some streaming data as and when it arrives. I want to be able to open multiple windows to monitor different streams.</p>
  
  <p>It works quite nicely until I start closing chart windows.</p>
  
  <p>The problem I have is that closing one of my chart windows frequently causes all windows to close and the application to exit. No error messages appear in the terminal, all of the windows simply close and the program terminates. Closing a chart window <em>should</em> only close the chart window.</p>
  
  <p>I'm using PyQt5, PyQtGraph 0.10.0 and Python 3.6.1 on Windows 10.</p>
  
  <p>The code below shows how my app is structured.</p>
  
  <ul>
  <li>The <strong>App</strong> class keeps track of the open windows and stores chart data. (In reality this class collects the streaming data and calls an update routine for the relevant open windows.)</li>
  <li>The <strong>ButtonWindow</strong> class can be used to make new chart windows. (In reality, there are many buttons depending on what the user wishes to chart.)</li>
  <li><p>The <strong>ChartWindow</strong> class displays the data.</p>
  
  <pre><code>import PyQt5.QtWidgets as qt
  import pyqtgraph as pg
  
  class App(qt.QApplication):
      def __init__(self,args):
          qt.QApplication.__init__(self,args)
  
          #window tracking
          self.last_idx = 0
          self.windows = {}
  
          #chart data
          self.x = [1,2,3,4,5]
          self.y = [1,2,3,4,5]
  
          #create button window
          self.button_window = ButtonWindow(self)
  
          #enter event loop
          self.exec_()
  
      def new_window(cls):
          cls.windows[cls.last_idx] = ChartWindow(cls, cls.last_idx)
          cls.last_idx += 1
  
      def close_window(cls, window_id):
          cls.windows[window_id].destroy()
          del cls.windows[window_id]
  
  class ButtonWindow(qt.QWidget):
      def __init__(self, app):
          qt.QWidget.__init__(self)
          self.grid = qt.QGridLayout()
          self.app = app
  
          #add a button
          self.btn = qt.QPushButton('+1 Chart Window')
          self.btn.clicked.connect(self.app.new_window)
          self.grid.addWidget(self.btn,0,0)
          self.setLayout(self.grid)
  
          #show window
          self.show()
  
  class ChartWindow(qt.QWidget):
      def __init__(self, app, window_id):
          qt.QWidget.__init__(self)
          self.grid = qt.QGridLayout()
          self.app = app
          self.window_id = window_id
          self.setWindowTitle('Chart Window '+str(self.window_id))
  
          #add a chart
          self.chart = pg.PlotWidget()
          self.chart.plot(app.x,app.y)
          self.grid.addWidget(self.chart,0,0)
          self.setLayout(self.grid)
  
          #show window
          self.show()
  
      def closeEvent(cls,event):
          #cls.chart.close()
          cls.app.close_window(cls.window_id)
  
  def main():
      app = App([])
  if __name__ == '__main__':
      main()
  </code></pre></li>
  </ul>
  
  <p>After reading around I think this is due to some conflict between the Python garbage collector and surviving references to the underlying c++ objects.</p>
  
  <ol>
  <li><a href="https://github.com/pyqtgraph/pyqtgraph/issues/55" rel="nofollow noreferrer">https://github.com/pyqtgraph/pyqtgraph/issues/55</a></li>
  <li><a href="https://stackoverflow.com/questions/35782474/pyqt-parent-for-widgets-necessary">parent for widgets - nescessary?</a></li>
  </ol>
  
  <p>The issue is definitely related to the PlotWidget. No crashes are observed if the plot widget is swapped for a button.</p>
  
  <p>I got the idea to add the 'cls.chart.close()' line in the closeEvent override from <a href="https://github.com/pyqtgraph/pyqtgraph/issues/55" rel="nofollow noreferrer">1</a>. This helps. Crashing becomes less frequent, but after 20 or so chart window closes, it will still occur.</p>
  
  <p>I've also tried making all widgets children of the window in which they reside, but this had no effect.</p>
  
  <p>Any ideas?  I can't believe something as simple as opening and closing a window containing a plot is enough to blow up PyQt so I assume I'm doing something stupid in my structure.</p>