Skip to content

Commit

Permalink
mujoco.MjrContext.free instead of relying on the destructor.
Browse files Browse the repository at this point in the history
Explicitly calling free() ensures that this operation happens within the right GL context.

PiperOrigin-RevId: 436226143
Change-Id: I556f621e88233e7a10ee5bec91a8cdfeab7c558c
  • Loading branch information
nimrod-gileadi authored and copybara-github committed Mar 21, 2022
1 parent ed9b601 commit f8ade5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dm_control/_render/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ def _free_at_exit():
def keep_alive(self, obj):
self._patients.append(obj)

def dont_keep_alive(self, obj):
try:
self._patients.remove(obj)
except ValueError:
pass

def increment_refcount(self):
self._refcount += 1

Expand All @@ -91,6 +97,8 @@ def _free_on_executor_thread(self): # pylint: disable=missing-function-docstrin
while self._patients:
patient = self._patients.pop()
assert sys.getrefcount(patient) == sys.getrefcount(dummy)
if hasattr(patient, 'free'):
patient.free()
del patient
finally:
self._platform_free()
Expand Down
7 changes: 7 additions & 0 deletions dm_control/mujoco/wrapper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ def free(self):
necessary. This MjrContext object MUST NOT be used after this function has
been called.
"""
if self._gl_context and not self._gl_context.terminated:
ptr = self.ptr
if ptr:
self._gl_context.dont_keep_alive(ptr)
with self._gl_context.make_current() as ctx:
ctx.call(ptr.free)

if self._gl_context:
self._gl_context.decrement_refcount()
self._gl_context.free()
Expand Down

0 comments on commit f8ade5d

Please sign in to comment.