pyopengl
pyopengl copied to clipboard
glTexImage2D leaks memory when numpy data is not contiguous
When a non-contiguous numpy array is passed to glTexImage2D(), or glBufferSubData() or probably any PyOpenGL routine that can take a numpy array it leaks memory. The reference count of the non-contiguous array is incremented and will then never be released. This must be related to PyOpenGL copying the numpy array to a contiguous one internally, then somehow forgetting to decref the original array which it increfed.
Here is example code (sorry only a fragment). If the data array is contiguous then its reference count before and after the glTexImage2D() call are the same. If it is not contiguous, the reference count of data after glTexImage2D() is one more than before.
from sys import getrefcount
rc1 = getrefcount(data)
GL.glTexImage2D(gl_target, 0, iformat, size0, size1, 0, format, tdtype, data)
rc2 = getrefcount(data)
print ('teximage2d ', data.flags['C_CONTIGUOUS'], rc1, rc2)
When updating textures frequently this leaks a massive amount of memory and is hard to debug. The case I encountered was rendering text as an image and the numpy array was flipped vertically data[::-1] so that it appeared right side up making it non-contiguous.
The issue of the memory leak, at least concerning glTexImage2D, seems to be fixed on the development branch of PyOpengl.