Skip to content

[Bug]: FigureCanvasQT is seemingly prematurely freed under certain conditions #29618

@ezra-varady

Description

@ezra-varady

Bug summary

I'm in the process of updating some code using matplotlib and pyside6. Using version 6.6.0 of PySide there is no error, but on versions 6.7.0 and newer I get an error indicating that the underlying C++ object has been prematurely freed. I've had a hard time identifying the source of the bug. I have opened an issue with pyside, but as I have been unable to recreate the bug without matplotlib components I thought I would open an issue here and see if anyone had any ideas. Sorry to bother, and thanks for the help.

Code for reproduction

from PySide6.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QApplication
from matplotlib.backends.backend_qt import FigureCanvasQT

from matplotlib.figure import Figure
class test_class(QMainWindow):
    def __init__(self, parent):
        super().__init__(parent)
        self.testfig = None
        self.canvas = FigureCanvasQT(self.testfig)
        self.vbox = QWidget(self)
        self.canvas.setParent(self.vbox)

def test_testing():
    qtbot = QApplication([])
    c1 = test_class(None)
    c1.setVisible(True)

test_testing()
QApplication.instance().processEvents()

Actual outcome

Traceback (most recent call last):
File "/home/ez/test.py", line 20, in
QApplication.instance().processEvents()
File "/home/ez/micromamba/envs/pycodas/lib/python3.11/site-packages/matplotlib/backends/backend_qt.py", line 495, in _draw_idle
if self.height() < 0 or self.width() < 0:
^^^^^^^^^^^^^
RuntimeError: Internal C++ object (FigureCanvasQT) already deleted.

Expected outcome

It shouldn't do anything, maybe briefly display a window, but the error is unexpected. I may just be using something wrong, but this code did run in the past without error

Additional information

I should mention that I copied the entire class definition of FigureCanvasQT into my test file and that runs just fine, which I find somewhat confusing. Though this may be explained by the fact that I am using version 3.9.1 of matplotlib which may differ from the main branch.

Operating system

ubuntu

Matplotlib Version

3.9.1

Matplotlib Backend

qtagg

Python version

3.11.11

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions