fixed various memory leaks/overruns/etc
I found many issues using AddressSanitizer and LeakSanitizer.
diff --git a/frc971/constants.cc b/frc971/constants.cc
index dd8583d..5be9b7e 100755
--- a/frc971/constants.cc
+++ b/frc971/constants.cc
@@ -4,9 +4,16 @@
#include <stdint.h>
#include <inttypes.h>
+#include <map>
+
+#if __has_feature(address_sanitizer)
+#include "sanitizer/lsan_interface.h"
+#endif
+
#include "aos/common/logging/logging.h"
#include "aos/common/once.h"
#include "aos/common/network/team_number.h"
+#include "aos/common/mutex.h"
#include "frc971/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
#include "frc971/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
@@ -191,7 +198,20 @@
}
const Values &GetValuesForTeam(uint16_t team_number) {
- return *(DoGetValuesForTeam(team_number));
+ static ::aos::Mutex mutex;
+ ::aos::MutexLocker locker(&mutex);
+
+ // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
+ // race conditions.
+ static ::std::map<uint16_t, const Values *> values;
+
+ if (values.count(team_number) == 0) {
+ values[team_number] = DoGetValuesForTeam(team_number);
+#if __has_feature(address_sanitizer)
+ __lsan_ignore_object(values[team_number]);
+#endif
+ }
+ return *values[team_number];
}
} // namespace constants