diff --git a/examples/minimal-mdns/client.cpp b/examples/minimal-mdns/client.cpp index 246a97252e6009..1a98bc539764df 100644 --- a/examples/minimal-mdns/client.cpp +++ b/examples/minimal-mdns/client.cpp @@ -301,6 +301,8 @@ void BroadcastPacket(mdns::Minimal::ServerBase * server) } } +mdns::Minimal::Server<20> gMdnsServer; + } // namespace int main(int argc, char ** args) @@ -324,17 +326,16 @@ int main(int argc, char ** args) printf("Running...\n"); - mdns::Minimal::Server<20> mdnsServer; ReportDelegate reporter; CHIP_ERROR err; - mdnsServer.SetDelegate(&reporter); + gMdnsServer.SetDelegate(&reporter); { MdnsExample::AllInterfaces allInterfaces(gOptions.enableIpV4); - err = mdnsServer.Listen(chip::DeviceLayer::UDPEndPointManager(), &allInterfaces, gOptions.listenPort); + err = gMdnsServer.Listen(chip::DeviceLayer::UDPEndPointManager(), &allInterfaces, gOptions.listenPort); if (err != CHIP_NO_ERROR) { printf("Server failed to listen on all interfaces: %s\n", chip::ErrorStr(err)); @@ -342,11 +343,15 @@ int main(int argc, char ** args) } } - BroadcastPacket(&mdnsServer); + BroadcastPacket(&gMdnsServer); err = DeviceLayer::SystemLayer().StartTimer( chip::System::Clock::Milliseconds32(gOptions.runtimeMs), [](System::Layer *, void *) { + // Close all sockets BEFORE system layer is shut down, otherwise + // attempts to free UDP sockets with system layer down will segfault + gMdnsServer.Shutdown(); + DeviceLayer::PlatformMgr().StopEventLoopTask(); DeviceLayer::PlatformMgr().Shutdown(); }, diff --git a/src/lib/dnssd/minimal_mdns/Server.cpp b/src/lib/dnssd/minimal_mdns/Server.cpp index b58c60cb3e463f..01b881f39c78fb 100644 --- a/src/lib/dnssd/minimal_mdns/Server.cpp +++ b/src/lib/dnssd/minimal_mdns/Server.cpp @@ -189,10 +189,7 @@ ServerBase::~ServerBase() void ServerBase::Shutdown() { - mEndpoints.ForEachActiveObject([&](auto * endpoint) { - ShutdownEndpoint(*endpoint); - return chip::Loop::Continue; - }); + mEndpoints.ReleaseAll(); } void ServerBase::ShutdownEndpoint(EndpointInfo & aEndpoint)