moderngl: Alpine Linux: Context.release() memory leak

Hello, I created a moderngl context in Python and called context.release(), but the memory never gets released. Here it is the code:

import gc
import moderngl.mgl as mgl
from moderngl.context import Context

ctx = Context.__new__(Context)
ctx.mglo, ctx.version_code = mgl.create_context(glversion=330, mode="standalone")
ctx.release()
del(ctx)
gc.collect()

image

There is approximately 50MB of memory that is not being released even after context.release(), del(context) or gc.collect(), so how can I retrieve this memory back? I would really appreciate any help on this!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 24 (13 by maintainers)

Most upvoted comments

Hello, @szabolcsdombi , I solved the git clone issue. Me and @ShootingStarDragon have been commenting every line from x11.cpp, compiling and profiling, and we suddenly found that the memory leak happens in GLXFBConfig * fbc, in which the memory decreased from 50MB to 3.3MB.

GLXFBConfig * fbc = res->m_glXChooseFBConfig(res->dpy, res->m_XDefaultScreen(res->dpy), 0, &nelements);

Before commenting this line:

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     3     40.6 MiB     40.6 MiB           1   @profile
     4                                         def test_memory_leak():
     5     40.6 MiB      0.0 MiB           1       import glcontext
     6     91.4 MiB     50.8 MiB           1       backend = glcontext.default_backend()(mode='standalone')

After commenting this line:

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     3     40.8 MiB     40.8 MiB           1   @profile
     4                                         def test_memory_leak():
     5     40.8 MiB      0.0 MiB           1       import glcontext
     6     44.2 MiB      3.4 MiB           1       backend = glcontext.default_backend()(mode='standalone')

We believe frame buffer context should be released at the end somehow

Some (hopefully useful) references: https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glXChooseFBConfig.xml

Use XFree to free the memory returned by glXChooseFBConfig.

https://www.khronos.org/opengl/wiki/Tutorial:_OpenGL_3.0_Context_Creation_(GLX) in this tutorial there is this comment

 // Be sure to free the FBConfig list allocated by glXChooseFBConfig()
  XFree( fbc );

we suddenly found that the memory leak happens in GLXFBConfig * fbc, in which the memory decreased from 50MB to 3.3MB.

good catch. I look into the docs how to release it.

I soon add this code to glcontext, you will be able to test as before.

I remember fixing a leak in the past and added a mass create test : https://github.com/moderngl/glcontext/blob/master/tests/default_context_test.py