Repository: pyqtgraph/pyqtgraph

Issue:
PR 942 introduces RuntimeError: wrapped C/C++ object of type GraphicsLayoutWidget has been deleted
Since commit 08c0de7, which merges PR #942, I get the following error when I close my application window.

`RuntimeError: wrapped C/C++ object of type GraphicsLayoutWidget has been deleted`

The following program reproduces the issue.
```

import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets
from PyQt5.Qt import PYQT_VERSION_STR


class DemoWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

        self.mainWidget = QtWidgets.QWidget()
        self.setCentralWidget(self.mainWidget)

        self.mainLayout = QtWidgets.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.plotItem = pg.PlotItem()
        self.plotItem.plot([3.5, 5, -4, 0, 0, 3])

        self.graphicsLayoutWidget = pg.GraphicsLayoutWidget()
        self.graphicsLayoutWidget.addItem(self.plotItem, 0, 0)

        self.mainLayout.addWidget(self.graphicsLayoutWidget)


def main():
    print(sys.version)
    print("PyQt version: {}".format(PYQT_VERSION_STR))
    print("PyQtGraph version: {}".format(pg.__version__))
    app = QtWidgets.QApplication([])
    win = DemoWindow()
    win.setGeometry(400, 100, 700, 600)
    win.show()
    app.exec()


if __name__ == '__main__':
    main()
```

I tested with PyQt 5.9 on OS-X 10.14.6.

```
3.6.7 | packaged by conda-forge | (default, Feb 28 2019, 02:16:08)  [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
PyQt version: 5.9.2
PyQtGraph version: 0.11.0.dev0
```

and also with PyQt 5.6

```
3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46) [GCC 4.2.1 Compatible Clang 4.0.1 tags/RELEASE_401/final)]
PyQt version: 5.6
PyQtGraph version: 0.11.0.dev0
```

In my opinion this PR will cause more issues than it solves. Messing with the python garbage collection will introduce a lot of unexpected issues like this one, while it doesn't solve a real bug, it only helps novice PyQt programmers.

Keeping references to your objects is one of the first things you learn as a PyQt programmer. Encountering this issue is so common that it is almost a rite of passage. There are plenty of StackOverflow posts that address it, just [googling it](https://www.google.com/search?client=firefox-b-d&q=pyqt+window+closes+immediately) already gives three links at the top. 

My personnel recommendation would be to be to roll back this PR and solve it in the documentation and the examples.



PR 942 introduces RuntimeError: wrapped C/C++ object of type GraphicsLayoutWidget has been deleted
Since commit 08c0de7, which merges PR #942, I get the following error when I close my application window.

`RuntimeError: wrapped C/C++ object of type GraphicsLayoutWidget has been deleted`

The following program reproduces the issue.
```

import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets
from PyQt5.Qt import PYQT_VERSION_STR


class DemoWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

        self.mainWidget = QtWidgets.QWidget()
        self.setCentralWidget(self.mainWidget)

        self.mainLayout = QtWidgets.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.plotItem = pg.PlotItem()
        self.plotItem.plot([3.5, 5, -4, 0, 0, 3])

        self.graphicsLayoutWidget = pg.GraphicsLayoutWidget()
        self.graphicsLayoutWidget.addItem(self.plotItem, 0, 0)

        self.mainLayout.addWidget(self.graphicsLayoutWidget)


def main():
    print(sys.version)
    print("PyQt version: {}".format(PYQT_VERSION_STR))
    print("PyQtGraph version: {}".format(pg.__version__))
    app = QtWidgets.QApplication([])
    win = DemoWindow()
    win.setGeometry(400, 100, 700, 600)
    win.show()
    app.exec()


if __name__ == '__main__':
    main()
```

I tested with PyQt 5.9 on OS-X 10.14.6.

```
3.6.7 | packaged by conda-forge | (default, Feb 28 2019, 02:16:08)  [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
PyQt version: 5.9.2
PyQtGraph version: 0.11.0.dev0
```

and also with PyQt 5.6

```
3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46) [GCC 4.2.1 Compatible Clang 4.0.1 tags/RELEASE_401/final)]
PyQt version: 5.6
PyQtGraph version: 0.11.0.dev0
```

In my opinion this PR will cause more issues than it solves. Messing with the python garbage collection will introduce a lot of unexpected issues like this one, while it doesn't solve a real bug, it only helps novice PyQt programmers.

Keeping references to your objects is one of the first things you learn as a PyQt programmer. Encountering this issue is so common that it is almost a rite of passage. There are plenty of StackOverflow posts that address it, just [googling it](https://www.google.com/search?client=firefox-b-d&q=pyqt+window+closes+immediately) already gives three links at the top. 

My personnel recommendation would be to be to roll back this PR and solve it in the documentation and the examples.





Discussion:
As the maintainer that merged PR #942 I agree with you @titusjan that we should roll this back, and use documentation and examples to highlight the issue that PR was meant to address.  Before I go undoing another person's PR myself, I'm going to want to get more of consensus from other maintainers here.

Tagging @ixjlyons @Dschoni @2xB  @hhslepicka for input.
Also unrelated to this issue, but @titusjan I advise you to be careful w/ Qt 5.6, we no longer test for it, and PySide2 that was running with Qt 5.6 was failing our test suite left and right.  
As the maintainer that merged PR #942 I agree with you @titusjan that we should roll this back, and use documentation and examples to highlight the issue that PR was meant to address.  Before I go undoing another person's PR myself, I'm going to want to get more of consensus from other maintainers here.

Tagging @ixjlyons @Dschoni @2xB  @hhslepicka for input.
Also unrelated to this issue, but @titusjan I advise you to be careful w/ Qt 5.6, we no longer test for it, and PySide2 that was running with Qt 5.6 was failing our test suite left and right.  

Resolving patch:
diff --git a/pyqtgraph/widgets/GraphicsView.py b/pyqtgraph/widgets/GraphicsView.py
--- a/pyqtgraph/widgets/GraphicsView.py
+++ b/pyqtgraph/widgets/GraphicsView.py
@@ -399,9 +399,12 @@ def dragEnterEvent(self, ev):
         ev.ignore()  ## not sure why, but for some reason this class likes to consume drag events
 
     def _del(self):
-        if self.parentWidget() is None and self.isVisible():
-            msg = "Visible window deleted. To prevent this, store a reference to the window object."
-            warnings.warn(msg, RuntimeWarning, stacklevel=2)
+        try:
+            if self.parentWidget() is None and self.isVisible():
+                msg = "Visible window deleted. To prevent this, store a reference to the window object."
+                warnings.warn(msg, RuntimeWarning, stacklevel=2)
+        except RuntimeError:
+            pass
 
 if sys.version_info[0] == 3 and sys.version_info[1] >= 4:
     GraphicsView.__del__ = GraphicsView._del


Test patch: