Change LoadModelFromString to return a smart pointer, add MakeData, and update tests to have C++ RAII clean up model and data.

PiperOrigin-RevId: 935980153
Change-Id: I41d25bfab4935494dc984168820cb7cad123cadf
This commit is contained in:
Kyle Bayes
2026-06-22 04:18:29 -07:00
committed by Copybara-Service
parent 34d142ee50
commit 1490336955
39 changed files with 4616 additions and 5908 deletions
+8 -10
View File
@@ -24,7 +24,7 @@
#include "test/fixture.h"
#ifdef MEMORY_SANITIZER
#include <sanitizer/msan_interface.h>
#include <sanitizer/msan_interface.h>
#endif
namespace mujoco {
@@ -38,7 +38,6 @@ using ::testing::NotNull;
using EnginePrintTest = MujocoTest;
constexpr const char* NullFile() {
#ifdef _WIN32
return "NUL";
@@ -60,27 +59,26 @@ TEST_F(EnginePrintTest, PrintDataWorksWithMsan) {
)";
std::array<char, 1024> error;
mjModel* model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model, NotNull()) << "Failed to load model: " << error.data();
mjData* data = mj_makeData(model);
MjModelPtr model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model.get(), NotNull())
<< "Failed to load model: " << error.data();
MjDataPtr data = MakeData(model);
ASSERT_THAT(data, NotNull());
// Mark qacc_smooth[0] as initialized.
data->qacc_smooth[0] = 0;
// This will read "uninitialized" values from mjData, but shouldn't fail.
mj_printData(model, data, NullFile());
mj_printData(model.get(), data.get(), NullFile());
// After mj_printData, poisoned status should be restored correctly, so
// qacc_smooth[0] should be marked initialzed.
EXPECT_EQ(data->qacc_smooth[0], 0);
#ifdef MEMORY_SANITIZER
EXPECT_THAT(__msan_test_shadow(data->buffer, data->nbuffer), Not(Eq(-1))) <<
"Expecting some of data->buffer to be marked uninitialized";
EXPECT_THAT(__msan_test_shadow(data->buffer, data->nbuffer), Not(Eq(-1)))
<< "Expecting some of data->buffer to be marked uninitialized";
#endif
mj_deleteData(data);
mj_deleteModel(model);
}
} // namespace