From 00b639516f8a0f6686fd98d805f88279cb07629d Mon Sep 17 00:00:00 2001 From: Amir Hamza Date: Wed, 10 Jun 2026 18:11:25 +0600 Subject: [PATCH] Fix memory leak and zombie routers in clearTop navigation Call detachInternal() on intermediate stack entries before removing them from the iterator in clearTop() to ensure correct lifecycle teardown and prevent memory leaks. --- .../src/main/kotlin/com/uber/rib/core/StackRouterNavigator.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/rib-router-navigator/src/main/kotlin/com/uber/rib/core/StackRouterNavigator.kt b/libraries/rib-router-navigator/src/main/kotlin/com/uber/rib/core/StackRouterNavigator.kt index 937a93832..430f4fb90 100644 --- a/libraries/rib-router-navigator/src/main/kotlin/com/uber/rib/core/StackRouterNavigator.kt +++ b/libraries/rib-router-navigator/src/main/kotlin/com/uber/rib/core/StackRouterNavigator.kt @@ -161,7 +161,7 @@ constructor( detachAll() newRouterAndState = buildNewState(newState, attachTransition, detachTransition) attachInternal(currentRouterAndState, newRouterAndState, true) - navigationStack.push(newRouterAndState) + navigationStack.push(newRouterAndState) } RouterNavigator.Flag.REPLACE_TOP -> { if (!navigationStack.isEmpty()) { @@ -309,6 +309,8 @@ constructor( attachInternal(currentRouterAndState, routerAndState, true) break } else { + // FIX: Correctly detaching zombie routers before removal to fix memory leaks (F-03) + detachInternal(routerAndState, newState, true) navigationIterator.remove() } }