Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Example: Core dump with PyQt4Core dump with PyQt4
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Example: Core dump with PyQt4
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Example: Core dump with PyQt4
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
added 80 characters in body
Source Link
Luke
  • 11.7k
  • 3
  • 54
  • 65
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Any Qt object that has both a parentExample: and a Python reference is a possible dangerCore dump with PyQt4
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Any Qt object that has both a parent and a Python reference is a possible danger
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Example: Core dump with PyQt4
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
added 549 characters in body
Source Link
Luke
  • 11.7k
  • 3
  • 54
  • 65
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Any Qt object that has both a parent and a Python reference is a possible danger
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
  • QGraphicsItems that are not part of a QGraphicsScene can cause crash on exit.
  • QObjects that reference their parent or any ancestor can cause an exit crash.
  • QGraphicsScene with no parent can cause an exit crash.
  • The easiest way to avoid exit crashes is to call os._exit() before python starts collecting Qt objects. However, this can be dangerous because some part of the program may be relying on proper exit handling to function correctly (for example, terminating log files or properly closing device handles). At a minimum, one should manually invoke the atexit callbacks before calling os._exit().
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
  • QGraphicsItems that are not part of a QGraphicsScene can cause crash on exit.
  • QObjects that reference their parent or any ancestor can cause an exit crash.
  • The easiest way to avoid exit crashes is to call os._exit() before python starts collecting Qt objects. However, this can be dangerous because some part of the program may be relying on proper exit handling to function correctly (for example, terminating log files or properly closing device handles). At a minimum, one should manually invoke the atexit callbacks before calling os._exit().
  • If you must use multi-threaded code, never-ever access the GUI from a non-GUI thread. Always instead send a message to the GUI thread by emitting a signal or some other thread-safe mechanism.
  • Be careful with Model/View anything. TableView, TreeView, etc. They are difficult to program correctly, and any mistakes lead to untraceable crashing. Use Model Test to help ensure your model is internally consistent.
  • Understand the way Qt object management interacts with Python object management and the cases where this can go wrong. See http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • Qt objects with no parent are "owned" by Python; only Python may delete them.
    • Qt objects with a parent are "owned" by Qt and will be deleted by Qt if their parent is deleted.
    • Any Qt object that has both a parent and a Python reference is a possible danger
  • A QObject should generally not have a reference to its parent or any of its ancestors (weak references are ok). This will cause memory leaks at best and occasional crashes as well.
  • Be aware of situations where Qt auto-deletes objects. If the python wrapper has not been informed that the C++ object was deleted, then accessing it will cause a crash. This can happen in many different ways due to the difficulty PyQt and PySide have in tracking Qt objects.
    • Compound widgets such as a QScrollArea and its scroll bars, QSpinBox and its QLineEdit, etc. (Pyside does not have this problem)

    • Deleting a QObject will automatically delete all of its children (however PyQt usually handles this correctly).

    • Removing items from QTreeWidget will cause any associated widgets (set with QTreeWidget.setItemWidget) to be deleted.

       # Example: from PyQt4 import QtGui, QtCore app = QtGui.QApplication([]) # Create a QScrollArea, get a reference to one of its scroll bars. w = QtGui.QWidget() sa = QtGui.QScrollArea(w) sb = sa.horizontalScrollBar() # Later on, we delete the top-level widget because it was removed from the # GUI and is no longer needed del w # At this point, Qt has automatically deleted all three widgets. # PyQt knows that the QScrollArea is gone and will raise an exception if # you try to access it: sa.parent() Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: underlying C/C++ object has been deleted # However, PyQt does not know that the scroll bar has also been deleted. # Since any attempt to access the deleted object will probably cause a # crash, this object is 'toxic'; remove all references to it to avoid # any accidents sb.parent() # Segmentation fault (core dumped) 
  • QGraphicsItems that are not part of a QGraphicsScene can cause crash on exit.
  • QObjects that reference their parent or any ancestor can cause an exit crash.
  • QGraphicsScene with no parent can cause an exit crash.
  • The easiest way to avoid exit crashes is to call os._exit() before python starts collecting Qt objects. However, this can be dangerous because some part of the program may be relying on proper exit handling to function correctly (for example, terminating log files or properly closing device handles). At a minimum, one should manually invoke the atexit callbacks before calling os._exit().
added workaround for exit crashes
Source Link
Luke
  • 11.7k
  • 3
  • 54
  • 65
Loading
added 639 characters in body
Source Link
Luke
  • 11.7k
  • 3
  • 54
  • 65
Loading
partial corrected the confusing reference to C++ reference being deleted underneath the python object
Source Link
jdi
  • 93k
  • 20
  • 174
  • 208
Loading
Source Link
Luke
  • 11.7k
  • 3
  • 54
  • 65
Loading
Post Made Community Wiki by Luke