From 44d6e4310cc9ad0d711d05e8dd5ee5220738e5b5 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 12 Dec 2023 03:43:49 -0800 Subject: [PATCH] Network invalidate handlers to cleanup in turbo module mode (#41775) Summary: We should do some cleanup for handlers in Networking to fix some memory leaks.Ex. `RCTHTTPRequestHandler` hander, `session` retains the handler which leads to leaks. https://github.com/facebook/react-native/blob/385473522cbc525aad08500f5a752dea734c14c3/packages/react-native/Libraries/Network/RCTHTTPRequestHandler.mm#L97 ## Changelog: [IOS] [FIXED] - Network invalidate handlers to cleanup in turbo module mode Pull Request resolved: https://github.com/facebook/react-native/pull/41775 Test Plan: Network handlers clean up after invalidating. Reviewed By: NickGerleman Differential Revision: D51846962 Pulled By: dmytrorykun fbshipit-source-id: 17a2875903cda187f2e3ecea46e8f7f3e10d5264 --- packages/react-native/Libraries/Network/RCTNetworking.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/react-native/Libraries/Network/RCTNetworking.mm b/packages/react-native/Libraries/Network/RCTNetworking.mm index ae78fdfb0d9e01..0eae618e898219 100644 --- a/packages/react-native/Libraries/Network/RCTNetworking.mm +++ b/packages/react-native/Libraries/Network/RCTNetworking.mm @@ -199,6 +199,11 @@ - (void)invalidate [_tasksByRequestID[requestID] cancel]; } [_tasksByRequestID removeAllObjects]; + for (id handler in _handlers) { + if ([handler conformsToProtocol:@protocol(RCTInvalidating)]) { + [(id)handler invalidate]; + } + } _handlers = nil; _requestHandlers = nil; _responseHandlers = nil;