From 3d1d133a48d7f33ba4d3661b46cb6ae6537ed3be Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Tue, 4 Aug 2026 05:44:01 -0700 Subject: [PATCH] In the compile sample, copy the error message before deleting the spec that owns it. PiperOrigin-RevId: 958970071 Change-Id: I1e8e367f26fde4778747020ab61f26105d70d690 --- sample/compile.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sample/compile.cc b/sample/compile.cc index d69543ea..41dd8d56 100644 --- a/sample/compile.cc +++ b/sample/compile.cc @@ -13,12 +13,12 @@ // limitations under the License. #include -#include #include #include #include #include #include +#include #include #include @@ -128,9 +128,10 @@ int main(int argc, char** argv) { } m = mj_compile(spec, &vfs); 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); - return finish(err_msg, EXIT_FAILURE, nullptr, &vfs); + return finish(err_msg.c_str(), EXIT_FAILURE, nullptr, &vfs); } if (argc == 2) { @@ -143,9 +144,9 @@ int main(int argc, char** argv) { // check error if (!m) { if (spec) { - auto err_msg = mjs_getError(spec); + std::string err_msg = mjs_getError(spec); mj_deleteSpec(spec); - return finish(err_msg, EXIT_FAILURE, nullptr, &vfs); + return finish(err_msg.c_str(), EXIT_FAILURE, nullptr, &vfs); } else { return finish("Could not load model", EXIT_FAILURE, nullptr, &vfs); }