Destroy peerconnections in test when they refer to on-stack mocks

Adds a function to PeerConnectionIntegrationBaseTest to stop and destroy
the caller and callee objects. This should take care of dangling pointers.

Before this change, the affected test would crash randomly - typically
detected within a few minutes of a gtest-repeat=-1 run.

After this change, it has not crashed in 15 minutes of running.

Bug: webrtc:12592
Change-Id: I9980f8974015bf2b2104fcb83c2ca0d677d03c3e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/264555
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37096}
diff --git a/pc/peer_connection_integrationtest.cc b/pc/peer_connection_integrationtest.cc
index 940d7fd..8afd807 100644
--- a/pc/peer_connection_integrationtest.cc
+++ b/pc/peer_connection_integrationtest.cc
@@ -1797,7 +1797,7 @@
 // Use a mock resolver to resolve the hostname back to the original IP on both
 // sides and check that the ICE connection connects.
 // TODO(bugs.webrtc.org/12590): Flaky on Windows and on Linux MSAN.
-#if defined(WEBRTC_WIN) || defined(WEBRTC_LINUX)
+#if defined(WEBRTC_WIN)
 #define MAYBE_IceStatesReachCompletionWithRemoteHostname \
   DISABLED_IceStatesReachCompletionWithRemoteHostname
 #else
@@ -1856,6 +1856,7 @@
   EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
                           "WebRTC.PeerConnection.CandidatePairType_UDP",
                           webrtc::kIceCandidatePairHostNameHostName));
+  DestroyPeerConnections();
 }
 
 #endif  // !defined(THREAD_SANITIZER)
diff --git a/pc/test/integration_test_helpers.h b/pc/test/integration_test_helpers.h
index a1d3a54..e733bea 100644
--- a/pc/test/integration_test_helpers.h
+++ b/pc/test/integration_test_helpers.h
@@ -1715,6 +1715,20 @@
 
   PeerConnectionIntegrationWrapper* caller() { return caller_.get(); }
 
+  // Destroy peerconnections.
+  // This can be used to ensure that all pointers to on-stack mocks
+  // get dropped before exit.
+  void DestroyPeerConnections() {
+    if (caller_) {
+      caller_->pc()->Close();
+    }
+    if (callee_) {
+      callee_->pc()->Close();
+    }
+    caller_.reset();
+    callee_.reset();
+  }
+
   // Set the `caller_` to the `wrapper` passed in and return the
   // original `caller_`.
   PeerConnectionIntegrationWrapper* SetCallerPcWrapperAndReturnCurrent(