In the compile sample, copy the error message before deleting the spec that owns it.

PiperOrigin-RevId: 958970071
Change-Id: I1e8e367f26fde4778747020ab61f26105d70d690
This commit is contained in:
Yuval Tassa
2026-08-04 05:44:01 -07:00
committed by Copybara-Service
parent 1e3c8f3f91
commit 3d1d133a48
+6 -5
View File
@@ -13,12 +13,12 @@
// limitations under the License. // limitations under the License.
#include <cctype> #include <cctype>
#include <chrono>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <string>
#include <string_view> #include <string_view>
#include <mujoco/mujoco.h> #include <mujoco/mujoco.h>
@@ -128,9 +128,10 @@ int main(int argc, char** argv) {
} }
m = mj_compile(spec, &vfs); m = mj_compile(spec, &vfs);
if (!m) { if (!m) {
auto err_msg = mjs_getError(spec); // copy the message before mj_deleteSpec frees the memory it points into
std::string err_msg = mjs_getError(spec);
mj_deleteSpec(spec); mj_deleteSpec(spec);
return finish(err_msg, EXIT_FAILURE, nullptr, &vfs); return finish(err_msg.c_str(), EXIT_FAILURE, nullptr, &vfs);
} }
if (argc == 2) { if (argc == 2) {
@@ -143,9 +144,9 @@ int main(int argc, char** argv) {
// check error // check error
if (!m) { if (!m) {
if (spec) { if (spec) {
auto err_msg = mjs_getError(spec); std::string err_msg = mjs_getError(spec);
mj_deleteSpec(spec); mj_deleteSpec(spec);
return finish(err_msg, EXIT_FAILURE, nullptr, &vfs); return finish(err_msg.c_str(), EXIT_FAILURE, nullptr, &vfs);
} else { } else {
return finish("Could not load model", EXIT_FAILURE, nullptr, &vfs); return finish("Could not load model", EXIT_FAILURE, nullptr, &vfs);
} }