Skip to content

Memory leak in mjs_delete when detaching a body #2882

@chengruiz

Description

@chengruiz

Intro

Hi!

I am a PhD student at Zhejiang University. I use MuJoCo for my research on legged locomotion.

I use massively parallel environments for reinforcement learning with procedural terrain generation. After upgrading MuJoCo from 3.3.3 to 3.3.4 (which version replaces mjs_detachBody with mjs_delete), I found that a continuous memory leak occurs that had never happened before. I checked the source of MuJoCo and found that in version 3.3.3, mjs_detachBody calls model->Detach(body);, where subtree->Release(); is invoked.

https://github.com/google-deepmind/mujoco/blob/f978b4eea368450267dd5e8825347653bec7355e/src/user/user_model.cc#L844C1-L850C1

However, after version 3.3.4, the implementation is moved to void mjCModel::operator-=(mjsElement* el), where the subtree->Release(); is removed. If I manually add subtree->Release();, the memory leak is fixed.

https://github.com/google-deepmind/mujoco/blob/8de86c9944e17efd04a3972a630a79a7da30736e/src/user/user_model.cc#L750C1-L757C6

Do I use the api in the wrong way? Or is this indeed a bug?

My setup

MuJoCo 3.3.3 - 3.3.6 C version

What's happening? What did you expect?

Memory leak.

Steps for reproduction

Simple code for reproduction may be hard to provide.

Minimal model for reproduction

No response

Code required for reproduction

No response

Confirmations

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions