Fix use after free in nightly tests

The OnShutdown handler was counting and dereferencing things, but the
variables it was using to do this weren't outliving the event loop.  The
destructor was doing the work, and ASAN caught it.

Change-Id: Ie21534f68d38efec7d6d6e9d1ac78fa1fb3d87d5
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/simulated_event_loop_test.cc b/aos/events/simulated_event_loop_test.cc
index 43c0c97..6b88ffe 100644
--- a/aos/events/simulated_event_loop_test.cc
+++ b/aos/events/simulated_event_loop_test.cc
@@ -1480,6 +1480,11 @@
 // Tests that rebooting a node changes the ServerStatistics message and the
 // RemoteTimestamp message.
 TEST_P(RemoteMessageSimulatedEventLoopTest, BootUUIDTest) {
+  const UUID pi1_boot0 = UUID::Random();
+  const UUID pi2_boot0 = UUID::Random();
+  const UUID pi2_boot1 = UUID::Random();
+  const UUID pi3_boot0 = UUID::Random();
+  UUID expected_boot_uuid = pi2_boot0;
 
   message_bridge::TestingTimeConverter time(
       configuration::NodesCount(&config.message()));
@@ -1493,10 +1498,6 @@
   const size_t pi3_index =
       configuration::GetNodeIndex(&config.message(), "pi3");
 
-  const UUID pi1_boot0 = UUID::Random();
-  const UUID pi2_boot0 = UUID::Random();
-  const UUID pi2_boot1 = UUID::Random();
-  const UUID pi3_boot0 = UUID::Random();
   {
     time.AddNextTimestamp(distributed_clock::epoch(),
                           {BootTimestamp::epoch(), BootTimestamp::epoch(),
@@ -1524,7 +1525,6 @@
 
   std::unique_ptr<EventLoop> pi1_remote_timestamp =
       pi1->MakeEventLoop("pi1_remote_timestamp");
-  UUID expected_boot_uuid = pi2_boot0;
 
   int timestamp_count = 0;
   pi1_remote_timestamp->MakeWatcher(
@@ -1625,6 +1625,11 @@
       aos::configuration::ReadConfig(
           ArtifactPath("aos/events/multinode_pingpong_test_split_config.json"));
 
+  size_t pi1_shutdown_counter = 0;
+  size_t pi2_shutdown_counter = 0;
+  MessageCounter<examples::Pong> *pi1_pong_counter = nullptr;
+  MessageCounter<examples::Ping> *pi2_ping_counter = nullptr;
+
   message_bridge::TestingTimeConverter time(
       configuration::NodesCount(&config.message()));
   SimulatedEventLoopFactory factory(&config.message());
@@ -1659,14 +1664,9 @@
   pi2->OnStartup([&pi2_startup_counter]() { ++pi2_startup_counter; });
 
   // Shutdown just counts.
-  size_t pi1_shutdown_counter = 0;
-  size_t pi2_shutdown_counter = 0;
   pi1->OnShutdown([&pi1_shutdown_counter]() { ++pi1_shutdown_counter; });
   pi2->OnShutdown([&pi2_shutdown_counter]() { ++pi2_shutdown_counter; });
 
-  MessageCounter<examples::Pong> *pi1_pong_counter = nullptr;
-  MessageCounter<examples::Ping> *pi2_ping_counter = nullptr;
-
   // Automatically make counters on startup.
   pi1->OnStartup([&pi1_pong_counter, pi1]() {
     pi1_pong_counter = pi1->AlwaysStart<MessageCounter<examples::Pong>>(
@@ -1758,6 +1758,11 @@
       aos::configuration::ReadConfig(
           ArtifactPath("aos/events/multinode_pingpong_test_split_config.json"));
 
+  int startup_count0 = 0;
+  int shutdown_count0 = 0;
+  int startup_count1 = 0;
+  int shutdown_count1 = 0;
+
   message_bridge::TestingTimeConverter time(
       configuration::NodesCount(&config.message()));
   SimulatedEventLoopFactory factory(&config.message());
@@ -1770,11 +1775,6 @@
 
   NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1");
 
-  int startup_count0 = 0;
-  int shutdown_count0 = 0;
-  int startup_count1 = 0;
-  int shutdown_count1 = 0;
-
   pi1->OnStartup([&]() { ++startup_count0; });
   pi1->OnShutdown([&]() { ++shutdown_count0; });
   EXPECT_EQ(startup_count0, 0);