2

This minimum working example of an environment with PyOpenGl and GLUT presents the same issue I am having on bigger code. Creating/closing continuously an instance of this class, increases memory usage until all my machine starts slowing down.

What is happening is that the call to glutDestroyWindow has not effect and the process /usr/lib/xorg/Xorg quickly fills up the whole GPU.

from OpenGL.GLUT import *

DISPLAY_WIDTH, DISPLAY_HEIGHT = 2000, 2000

class TestEnv:
    def __init__(self):
        self.window = None
        glutInit(sys.argv)
        glutInitWindowSize(DISPLAY_WIDTH, DISPLAY_HEIGHT)
        self.window = glutCreateWindow(b"TestEnv")

    def close(self):
        if self.window:
            glutDestroyWindow(self.window)

if __name__ == "__main__":
    i = 0
    while True:
        env = TestEnv()
        env.close()
        print(i)
        i+=1

What is the correct way of releasing all resources?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Daniele
  • 21
  • 2
  • I don't use glut, but it looks like you are freeing the window instance, but not the memory occupied by `glutInit(sys.argv)`? Maybe surf the glut docs from some kind of glutDone()? – Mudkip Hacker Mar 05 '21 at 22:13

1 Answers1

0

PyOpenGL uses freeglut. You have to give freeglut the chance to close the window. glutDestroyWindow does not destroy the window immediately, but triggers an event that destroys the window. There for you have to run the event loop. Invoke glutMainLoopEvent after requesting the window to be destroyed:

from OpenGL.GLUT import *

DISPLAY_WIDTH, DISPLAY_HEIGHT = 2000, 2000

class TestEnv:
    def __init__(self):
        self.window = None
        glutInit(sys.argv)
        glutInitWindowSize(DISPLAY_WIDTH, DISPLAY_HEIGHT)
        self.window = glutCreateWindow(b"TestEnv")

    def close(self):
        if self.window:
            glutDestroyWindow(self.window)

if __name__ == "__main__":
    i = 0
    while True:
        env = TestEnv()
        env.close()
        glutMainLoopEvent() # <--- handle events
        print(i)
        i += 1

See also Immediate mode and legacy OpenGL

Rabbid76
  • 202,892
  • 27
  • 131
  • 174