Modify user values in mujoco to allow users to provide cleanup functions to avoid memory leaks.

For the MJCF -> USD plugin, and I suspect other usecases for user values, it's necessary to give ownership of the object to Mujoco. However since they get type erased, we can't clean up the data automatically for the user. Instead this allows c++ clients to provide a cleanup function with their data.

PiperOrigin-RevId: 756832010
Change-Id: I80b8e7822e1a0e399a0d19dcaa57ee69b3ccc16a
This commit is contained in:
Sam Haves
2025-05-09 11:02:09 -07:00
committed by Copybara-Service
parent baf84265b8
commit 924ee3070a
9 changed files with 92 additions and 15 deletions
@@ -99,8 +99,13 @@ class MjFunctionVisitor:
return ''.join(strings)
def visit(self, node: ClangJsonNode) -> None:
if (node.get('kind') == 'FunctionDecl' and
node.get('name', '').startswith('mj')):
# Skip mjs_setUserValueWithCleanup as it's only useful for heap allocated
# objects and doesn't need a python wrapper.
if (
node.get('kind') == 'FunctionDecl'
and node.get('name', '').startswith('mj')
and node.get('name', '') != 'mjs_setUserValueWithCleanup'
):
func_decl = self._make_function(node)
self._functions[func_decl.name] = func_decl