Fix memory leak in mjCModel::Compile.

This is a very subtle leak caused interaction between longjmp and compiler optimisation. Specifically, at the point where the setjmp takes places, these pointers have never been reassigned from its nullptr initialization. Without the volatile keyword, the compiler is free to assume that these pointers remain nullptr when the setjmp returns, and therefore to pass nullptr directly to the mj_deleteModel and mj_deleteData calls in the subsequent catch block, without ever reading the actual pointer values.

PiperOrigin-RevId: 466649447
Change-Id: I51e82ec8b566deb144221e11a99d5124a8267de4
This commit is contained in:
Saran Tunyasuvunakool
2022-08-10 05:11:15 -07:00
committed by Copybara-Service
parent c2aff3a9f2
commit e3ded23c66
2 changed files with 316 additions and 305 deletions
+2
View File
@@ -153,6 +153,8 @@ class mjCModel {
int nuser_sensor; // number of mjtNums in sensor_user
private:
void TryCompile(mjModel** m, mjData** data, const mjVFS* vfs);
void Clear(void); // clear objects allocated by Compile
template <class T> // add object of any type