diff --git a/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.cpp b/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.cpp index 90f19df8dde..920973fa027 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.cpp @@ -30,6 +30,21 @@ std::shared_ptr InstanceTarget::createAgent( return instanceAgent; } +void InstanceTarget::removeExpiredAgents() { + // Remove all expired agents. + forEachAgent([](auto&) {}); +} + +InstanceTarget::~InstanceTarget() { + removeExpiredAgents(); + + // Agents are owned by the session, not by InstanceTarget, but + // they hold an InstanceTarget& that we must guarantee is valid. + assert( + agents_.empty() && + "InstanceAgent objects must be destroyed before their InstanceTarget. Did you call PageTarget::unregisterInstance()?"); +} + RuntimeTarget& InstanceTarget::registerRuntime( RuntimeTargetDelegate& delegate) { assert(!currentRuntime_ && "Only one Runtime allowed"); diff --git a/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.h b/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.h index ae2ed58567a..53a215415f3 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/InstanceTarget.h @@ -53,6 +53,7 @@ class InstanceTarget { InstanceTarget(InstanceTarget&&) = delete; InstanceTarget& operator=(const InstanceTarget&) = delete; InstanceTarget& operator=(InstanceTarget&&) = delete; + ~InstanceTarget(); std::shared_ptr createAgent( FrontendChannel channel,