Skip to content

Memory Leak when adding GLScatterPlotItem #103

@jdreaver

Description

@jdreaver

I have found that adding and removing the same GLScatterPlotItem in a GLViewWidget causes a memory leak. Here is a script to demonstrate the behavior:

import numpy as np
from PySide import QtCore, QtGui
import sys

import pyqtgraph.opengl as gl

num_points = 10
refresh_interval = 1  # ms


class PlotWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        # Basic layout setup
        self.viewWidget = gl.GLViewWidget()
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.viewWidget)
        self.setLayout(layout)

        # Create out scatter plot item. Note that this leak only happens with
        # scatter items, not with line or mesh items.
        pos = np.random.rand(num_points, 3)
        self.scatter = gl.GLScatterPlotItem(pos=pos)
        self.viewWidget.addItem(self.scatter)

        # Create and run timer to repeatedly add and remove scatter item.
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self._add_remove)
        self.timer.start(refresh_interval)

    def _add_remove(self):
        self.viewWidget.addItem(self.scatter)
        self.viewWidget.removeItem(self.scatter)

    def sizeHint(self):
        return QtCore.QSize(800, 800)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    widget = PlotWidget()
    widget.show()

    sys.exit(app.exec_())

I have tried using other plot items (like GLLinePlotItem and GLAxisItem), and I do not see the memory leak, so it seems only GLScatterPlotItem creates the leak. I have also tried using PyQt4 and the script still generates a memory leak.

Could this be a problem with binding data to the OpenGL context, but then that data does not get released when the plot item is removed? This is my current theory, because I used the objgraph package to find new Python objects every few seconds, and it couldn't find any. Furthermore, GLScatterPlotItem is the only item I tested that has a non-trivial initializeGL method.

I will do more investigation. If there is a workaround, I would appreciate that too :)

Thanks in advance for any help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions