Skip to content

Commit

Permalink
Fix SEGFAULT when running minmdns client (#13423)
Browse files Browse the repository at this point in the history
* Fix SEGFAULT when running minmdns client

* Fix typo
  • Loading branch information
andy31415 authored and pull[bot] committed Mar 29, 2022
1 parent 146bb97 commit 1035330
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 9 additions & 4 deletions examples/minimal-mdns/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ void BroadcastPacket(mdns::Minimal::ServerBase * server)
}
}

mdns::Minimal::Server<20> gMdnsServer;

} // namespace

int main(int argc, char ** args)
Expand All @@ -324,29 +326,32 @@ 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));
return 1;
}
}

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();
},
Expand Down
5 changes: 1 addition & 4 deletions src/lib/dnssd/minimal_mdns/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1035330

Please sign in to comment.