Modify user values in mujoco to allow users to provide cleanup functions to avoid memory leaks.
For the MJCF -> USD plugin, and I suspect other usecases for user values, it's necessary to give ownership of the object to Mujoco. However since they get type erased, we can't clean up the data automatically for the user. Instead this allows c++ clients to provide a cleanup function with their data. PiperOrigin-RevId: 756832010 Change-Id: I80b8e7822e1a0e399a0d19dcaa57ee69b3ccc16a
This commit is contained in:
committed by
Copybara-Service
parent
baf84265b8
commit
924ee3070a
@@ -784,15 +784,14 @@ void mjCBase::SetFrame(mjCFrame* _frame) {
|
||||
frame = _frame;
|
||||
}
|
||||
|
||||
|
||||
void mjCBase::SetUserValue(std::string_view key, const void* data) {
|
||||
user_payload_[std::string(key)] = data;
|
||||
void mjCBase::SetUserValue(std::string_view key, const void* data,
|
||||
void (*cleanup)(const void*)) {
|
||||
user_payload_[std::string(key)] = UserValue(data, cleanup);
|
||||
}
|
||||
|
||||
|
||||
const void* mjCBase::GetUserValue(std::string_view key) {
|
||||
auto found = user_payload_.find(std::string(key));
|
||||
return found != user_payload_.end() ? found->second : nullptr;
|
||||
return found != user_payload_.end() ? found->second.value : nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user