diff --git a/examples/platform/nrfconnect/util/test/TestInetCommon.cpp b/examples/platform/nrfconnect/util/test/TestInetCommon.cpp index 817250157ed7a6..4c6ad877fd7bda 100644 --- a/examples/platform/nrfconnect/util/test/TestInetCommon.cpp +++ b/examples/platform/nrfconnect/util/test/TestInetCommon.cpp @@ -76,6 +76,11 @@ void InitTestInetCommon() chip::Platform::MemoryInit(); } +void ShutdownTestInetCommon() +{ + chip::Platform::MemoryShutdown(); +} + void InitSystemLayer() { gSystemLayer.Init(); diff --git a/src/app/CASESessionManager.h b/src/app/CASESessionManager.h index 00b2651e20fa32..2e499595426342 100644 --- a/src/app/CASESessionManager.h +++ b/src/app/CASESessionManager.h @@ -63,6 +63,7 @@ class CASESessionManager : public Dnssd::OperationalResolveDelegate virtual ~CASESessionManager() { mDNSResolver.Shutdown(); } CHIP_ERROR Init(); + void Shutdown() { mDNSResolver.Shutdown(); } /** * Find an existing session for the given node ID, or trigger a new session request. diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index f77f97dcaa02c3..961131a05ddd71 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -344,6 +344,7 @@ void Server::Shutdown() mSessions.Shutdown(); mTransports.Close(); mCommissioningWindowManager.Shutdown(); + mCASESessionManager.Shutdown(); chip::Platform::MemoryShutdown(); } diff --git a/src/app/tests/TestCommissionManager.cpp b/src/app/tests/TestCommissionManager.cpp index 54d623e93c7d2b..e594fb54172e3a 100644 --- a/src/app/tests/TestCommissionManager.cpp +++ b/src/app/tests/TestCommissionManager.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -52,6 +53,27 @@ void InitializeChip(nlTestSuite * suite) chip::DeviceLayer::PlatformMgr().StartEventLoopTask(); } +void ShutdownChipTest() +{ + chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); + chip::DeviceLayer::PlatformMgr().Shutdown(); + + auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance(); + mdnsAdvertiser.RemoveServices(); + mdnsAdvertiser.Shutdown(); + + // Server shudown will be called in TearDownTask + + // TODO: At this point UDP endpoits still seem leaked and the sanitizer + // builds will attempt a memory free. As a result, we keep Memory initialized + // so that the global UDPManager can still be destructed without a coredump. + // + // This is likely either a missing shutdown or an actual UDP endpoint leak + // which I have not been able to track down yet. + // + // chip::Platform::MemoryShutdown(); +} + void CheckCommissioningWindowManagerBasicWindowOpenCloseTask(intptr_t context) { nlTestSuite * suite = reinterpret_cast(context); @@ -182,8 +204,7 @@ int TestCommissioningWindowManager() // TODO: The platform memory was intentionally left not deinitialized so that minimal mdns can destruct chip::DeviceLayer::PlatformMgr().ScheduleWork(TearDownTask, 0); sleep(kTestTaskWaitSeconds); - chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); - chip::DeviceLayer::PlatformMgr().Shutdown(); + ShutdownChipTest(); return (nlTestRunnerStats(&theSuite)); } diff --git a/src/inet/tests/TestInetCommon.h b/src/inet/tests/TestInetCommon.h index 6c4d1dc33578d0..3665f3938967d8 100644 --- a/src/inet/tests/TestInetCommon.h +++ b/src/inet/tests/TestInetCommon.h @@ -62,6 +62,7 @@ extern bool gDone; void InitTestInetCommon(); void InitSystemLayer(); void ShutdownSystemLayer(); +void ShutdownTestInetCommon(); void InetFailError(CHIP_ERROR err, const char * msg); void InitNetwork(); diff --git a/src/inet/tests/TestInetCommonPosix.cpp b/src/inet/tests/TestInetCommonPosix.cpp index 57494e2486986e..3a43ac4cfdbae7 100644 --- a/src/inet/tests/TestInetCommonPosix.cpp +++ b/src/inet/tests/TestInetCommonPosix.cpp @@ -154,6 +154,11 @@ void InitTestInetCommon() UseStdoutLineBuffering(); } +void ShutdownTestInetCommon() +{ + chip::Platform::MemoryShutdown(); +} + void InitSystemLayer() { #if CHIP_SYSTEM_CONFIG_USE_LWIP diff --git a/src/inet/tests/TestInetLayer.cpp b/src/inet/tests/TestInetLayer.cpp index 82f3f35091d0fd..2486c0ff0fe3c5 100644 --- a/src/inet/tests/TestInetLayer.cpp +++ b/src/inet/tests/TestInetLayer.cpp @@ -320,6 +320,8 @@ int main(int argc, char * argv[]) lSuccessful = Common::WasSuccessful(sTestState.mStatus); + ShutdownTestInetCommon(); + exit: return (lSuccessful ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/src/lib/dnssd/minimal_mdns/tests/TestAdvertiser.cpp b/src/lib/dnssd/minimal_mdns/tests/TestAdvertiser.cpp index 12691431b98208..65ad23b7e505e4 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestAdvertiser.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestAdvertiser.cpp @@ -559,6 +559,10 @@ int TestAdvertiser(void) nlTestRunner(&theSuite, &server); server.Shutdown(); context.Shutdown(); + mdnsAdvertiser.RemoveServices(); + mdnsAdvertiser.Shutdown(); + chip::Platform::MemoryShutdown(); + return nlTestRunnerStats(&theSuite); } diff --git a/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp b/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp index 259ae14806120d..b9febe27d1a7b0 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp @@ -306,12 +306,22 @@ const nlTest sTests[] = { NL_TEST_SENTINEL() // }; +int TestSetup(void * inContext) +{ + return chip::Platform::MemoryInit() == CHIP_NO_ERROR ? SUCCESS : FAILURE; +} + +int TestTeardown(void * inContext) +{ + chip::Platform::MemoryShutdown(); + return SUCCESS; +} + } // namespace int TestMinimalMdnsAllocator(void) { - chip::Platform::MemoryInit(); - nlTestSuite theSuite = { "MinimalMdnsAllocator", &sTests[0], nullptr, nullptr }; + nlTestSuite theSuite = { "MinimalMdnsAllocator", &sTests[0], &TestSetup, &TestTeardown }; nlTestRunner(&theSuite, nullptr); return nlTestRunnerStats(&theSuite); } diff --git a/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp b/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp index 2fdab46220a2ee..b97b2e4b269853 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp @@ -354,12 +354,22 @@ const nlTest sTests[] = { NL_TEST_SENTINEL() // }; +int TestSetup(void * inContext) +{ + return chip::Platform::MemoryInit() == CHIP_NO_ERROR ? SUCCESS : FAILURE; +} + +int TestTeardown(void * inContext) +{ + chip::Platform::MemoryShutdown(); + return SUCCESS; +} + } // namespace int TestResponseSender(void) { - chip::Platform::MemoryInit(); - nlTestSuite theSuite = { "RecordData", sTests, nullptr, nullptr }; + nlTestSuite theSuite = { "RecordData", sTests, &TestSetup, &TestTeardown }; nlTestRunner(&theSuite, nullptr); return nlTestRunnerStats(&theSuite); } diff --git a/src/lib/dnssd/platform/tests/TestPlatform.cpp b/src/lib/dnssd/platform/tests/TestPlatform.cpp index 097cb8b26346ad..3823e1b474c754 100644 --- a/src/lib/dnssd/platform/tests/TestPlatform.cpp +++ b/src/lib/dnssd/platform/tests/TestPlatform.cpp @@ -221,7 +221,12 @@ void TestCommissionableNode(nlTestSuite * inSuite, void * inContext) int TestSetup(void * inContext) { - chip::Platform::MemoryInit(); + return chip::Platform::MemoryInit() == CHIP_NO_ERROR ? SUCCESS : FAILURE; +} + +int TestTeardown(void * inContext) +{ + chip::Platform::MemoryShutdown(); return SUCCESS; } @@ -236,7 +241,7 @@ const nlTest sTests[] = { int TestDnssdPlatform(void) { - nlTestSuite theSuite = { "DnssdPlatform", &sTests[0], &TestSetup, nullptr }; + nlTestSuite theSuite = { "DnssdPlatform", &sTests[0], &TestSetup, &TestTeardown }; nlTestRunner(&theSuite, nullptr); return nlTestRunnerStats(&theSuite); } diff --git a/src/lib/support/tests/TestCHIPArgParser.cpp b/src/lib/support/tests/TestCHIPArgParser.cpp index 4ba1f9fe8ccc79..1b266925790f2d 100644 --- a/src/lib/support/tests/TestCHIPArgParser.cpp +++ b/src/lib/support/tests/TestCHIPArgParser.cpp @@ -788,6 +788,8 @@ int TestCHIPArgParser(void) printf("All tests succeeded\n"); + chip::Platform::MemoryShutdown(); + return (EXIT_SUCCESS); } #else // CHIP_CONFIG_ENABLE_ARG_PARSER diff --git a/src/platform/tests/TestCHIPoBLEStackMgr.cpp b/src/platform/tests/TestCHIPoBLEStackMgr.cpp index 194128a7577c8f..168301c4285c53 100644 --- a/src/platform/tests/TestCHIPoBLEStackMgr.cpp +++ b/src/platform/tests/TestCHIPoBLEStackMgr.cpp @@ -52,8 +52,8 @@ int TestCHIPoBLEStackManager() ChipLogProgress(DeviceLayer, "Start Chip Over Ble stack Done"); chip::DeviceLayer::PlatformMgrImpl().RunEventLoop(); - ChipLogProgress(DeviceLayer, "Start EventLoop"); - + ChipLogProgress(DeviceLayer, "RunEventLoop completed"); + chip::Platform::MemoryShutdown(); return 0; } diff --git a/src/platform/tests/TestThreadStackMgr.cpp b/src/platform/tests/TestThreadStackMgr.cpp index ba1bb7d1a5b5c4..b7a320ce4b15bf 100644 --- a/src/platform/tests/TestThreadStackMgr.cpp +++ b/src/platform/tests/TestThreadStackMgr.cpp @@ -46,6 +46,7 @@ void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg { if (event->ThreadConnectivityChange.Result == chip::DeviceLayer::ConnectivityChange::kConnectivity_Established) { + chip::Platform::MemoryShutdown(); exit(0); } } @@ -76,6 +77,7 @@ int TestThreadStackManager() printf("Start Thread task done\n"); chip::DeviceLayer::PlatformMgrImpl().RunEventLoop(); + chip::Platform::MemoryShutdown(); return -1; } diff --git a/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp b/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp index 6f08bd854bf686..2a9528436b098c 100644 --- a/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp +++ b/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp @@ -170,6 +170,7 @@ int Finalize(void * aContext) { CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); gIOContext.Shutdown(); + chip::Platform::MemoryShutdown(); return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; } diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp index 827036b5a1a3b9..7969b139df49d6 100644 --- a/src/system/tests/TestSystemPacketBuffer.cpp +++ b/src/system/tests/TestSystemPacketBuffer.cpp @@ -235,6 +235,8 @@ int PacketBufferTest::TestTeardown(void * inContext) if (err != CHIP_NO_ERROR && err != CHIP_ERROR_NOT_IMPLEMENTED) return FAILURE; + chip::Platform::MemoryShutdown(); + return SUCCESS; }