Fix a memory leak when copy.deepcopy(data) is called.

In MjDataWrapper.__deepcopy__ use the Python implementation of MjModelWrapper.__deepcopy__ to make an object that Python knows about and can release later.

Fixes google-deepmind/mujoco#1572.

PiperOrigin-RevId: 644967999
Change-Id: I95d093c672e136122e1939f69463a4d29dad82e8
This commit is contained in:
Nimrod Gileadi
2024-06-20 03:45:26 -07:00
committed by Copybara-Service
parent 3f3b39bbb1
commit 2188cba4cd
3 changed files with 78 additions and 3 deletions
+5 -3
View File
@@ -1907,9 +1907,11 @@ This is useful for example when the MJB is not available as a file on disk.)"));
mjData.def("__copy__", [](const MjDataWrapper& other) {
return MjDataWrapper(other);
});
mjData.def("__deepcopy__", [](const MjDataWrapper& other, py::dict) {
MjModelWrapper* model_copy = new MjModelWrapper(other.model());
return MjDataWrapper(other, model_copy);
mjData.def("__deepcopy__", [](const MjDataWrapper& other, py::dict memo) {
// Use copy.deepcopy(model) to make a model that Python is aware of.
py::object new_model_py =
py::cast(other.model()).attr("__deepcopy__")(memo);
return MjDataWrapper(other, new_model_py.cast<MjModelWrapper*>());
});
mjData.def(py::pickle(
[](const MjDataWrapper& d) { // __getstate__