Fix crash when simulated event loop gets destroyed before OnRun called
We were saving a std::function for each OnRun callback inside the
EventScheduler, and continuing to call it regardless of if the EventLoop
which added it was still alive. This was triggering segfaults when
trying to access the destroyed event loop. Instead, save a pointer to
the event loop, and clean it up when the event loop is destroyed so this
works as the user would expect.
While we are here, and since the behavior needs to change as part of
this change, only let OnRun callbacks be scheduled before the event loop
has run any. Test this too.
Change-Id: I467a00c9f0981669e77fd03927ceeb149a79e6c4
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/y2023/vision/april_detection_test.cc b/y2023/vision/april_detection_test.cc
index fea5105..71e34cb 100644
--- a/y2023/vision/april_detection_test.cc
+++ b/y2023/vision/april_detection_test.cc
@@ -35,7 +35,9 @@
constants_sender_(receive_pose_event_loop_.get(),
"y2023/constants/constants.json", 7971, "/constants"),
detector_(
- AprilRoboticsDetector(send_pose_event_loop_.get(), "/camera")) {}
+ AprilRoboticsDetector(send_pose_event_loop_.get(), "/camera")) {
+ event_loop_factory_.RunFor(std::chrono::milliseconds(5));
+ }
void SendImage(std::string path) {
aos::FlatbufferVector<frc971::vision::CameraImage> image =
@@ -51,7 +53,7 @@
void TestDistanceAngle(std::string image_path, double expected_distance,
double expected_angle) {
- receive_pose_event_loop_->OnRun([&]() { SendImage(image_path); });
+ SendImage(image_path);
event_loop_factory_.RunFor(std::chrono::milliseconds(5));
ASSERT_TRUE(april_pose_fetcher_.Fetch());