diff --git a/components/openscreen_platform/udp_socket.cc b/components/openscreen_platform/udp_socket.cc index 1fc4bf3a69d51..8a401c002238a 100644 --- a/components/openscreen_platform/udp_socket.cc +++ b/components/openscreen_platform/udp_socket.cc @@ -152,6 +152,10 @@ void UdpSocket::OnReceived( int32_t net_result, const std::optional& source_endpoint, std::optional> data) { + // |client_->OnRead| may synchronously destroy |this| (e.g. via the Cast + // receiver Environment::OnError -> ... -> StopApplication teardown chain). + // Guard the trailing member access with a weak pointer. + base::WeakPtr weak_this = weak_ptr_factory_.GetWeakPtr(); if (net_result != net::OK) { client_->OnRead(this, Error::Code::kSocketReadFailure); } else if (data) { @@ -163,6 +167,9 @@ void UdpSocket::OnReceived( client_->OnRead(this, std::move(packet)); } + if (!weak_this) { + return; + } udp_socket_->ReceiveMore(1); }