Repository: flutter
Issue: Multiple flutters with same engine group freeze with platform view
Labels: platform-ios,engine,a: platform-views,has reproducible steps,P2,team-ios,triaged-ios,found in release: 3.24,found in release: 3.27

### Steps to reproduce

1. Clone the sample repo
2. Run `flutter pub get` inside `freeze`
3. Run `flutter precache --ios` inside `freeze`
3. Run `pod install` in the root of the repo
4. Open `FlutterExperiments.xcworkspace`
5. Run the app
6. Scroll the home tab
7. Go to the profile tab
8. Scroll the profile tab
9. Repeat 6 to 8 a couple times. Home screen should freeze

The issue is only reproducible on iOS

### Expected results

Scroll keeps working

### Actual results

The screen freezes

### Code sample

We are providing a full repo because it fits better to showcase the integration between multiple flutters and native views: https://github.com/motain/flutter-experiments

But in any case, here is the minimal code example

<details open>
<summary>iOS driver</summary>
    
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
        let engines = FlutterEngineGroup(name: "multiple-flutters", project: nil)
        var window: UIWindow?
        
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let scene = (scene as? UIWindowScene) else { return }
            let window = UIWindow(windowScene: scene)
            let tabBar = UITabBarController()
            window.rootViewController = tabBar
            
            tabBar.viewControllers = [
                SingleFlutterViewController(withRoute: "/home", withEngineGroup: engines),
                SingleFlutterViewController(withRoute: "/profile", withEngineGroup: engines),
            ]
            
            window.makeKeyAndVisible()
            self.window = window
        }
    }
    
    final class SingleFlutterViewController: FlutterViewController {
        private var channel: FlutterMethodChannel?
    
        init(withRoute initialRoute: String, withEngineGroup engineGroup: FlutterEngineGroup) {
            let newEngine = engineGroup.makeEngine(withEntrypoint: nil, libraryURI: nil, initialRoute: initialRoute)
            GeneratedPluginRegistrant.register(with: newEngine)
            super.init(engine: newEngine, nibName: nil, bundle: nil)
    
            tabBarItem = UITabBarItem(title: initialRoute, image: nil, tag: initialRoute.hashValue)
        }
    
        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
</details>

<details open>
<summary>iOS Plugin</summary>


    public class FreezerPlugin: NSObject, FlutterPlugin {
      public static func register(with registrar: FlutterPluginRegistrar) {
          let factory = FLNativeViewFactory(messenger: registrar.messenger())
          registrar.register(factory, withId: "freezer")
      }
    }
    
    
    class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
        private var messenger: FlutterBinaryMessenger
    
        init(messenger: FlutterBinaryMessenger) {
            self.messenger = messenger
            super.init()
        }
    
        func create(
            withFrame frame: CGRect,
            viewIdentifier viewId: Int64,
            arguments args: Any?
        ) -> FlutterPlatformView {
            return FLNativeView(
                frame: frame,
                viewIdentifier: viewId,
                arguments: args,
                binaryMessenger: messenger)
        }
    
        public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
              return FlutterStandardMessageCodec.sharedInstance()
        }
    }
    
    class FLNativeView: NSObject, FlutterPlatformView {
        private var _view: UIView
    
        init(
            frame: CGRect,
            viewIdentifier viewId: Int64,
            arguments args: Any?,
            binaryMessenger messenger: FlutterBinaryMessenger?
        ) {
            _view = UIView()
            super.init()
            createNativeView(view: _view)
        }
    
        func view() -> UIView {
            return _view
        }
    
        func createNativeView(view _view: UIView){
            _view.backgroundColor = UIColor.blue
            let nativeLabel = UILabel()
            nativeLabel.text = "Native text from iOS"
            nativeLabel.textColor = UIColor.white
            nativeLabel.textAlignment = .center
            nativeLabel.frame = CGRect(x: 0, y: 0, width: 180, height: 48.0)
            _view.addSubview(nativeLabel)
        }
    }
    
</details>


<details open>
<summary>Flutter</summary>

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    Future<void> main() async {
      runApp(MyApp());
    }
    
    get fakeClubId => "1094";
    
    get fakeNationalId => "1";
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Home',
          initialRoute: '/home',
          routes: {
            // When navigating to the "/" route, build the FirstScreen widget.
            '/home': (context) => Scaffold(
                    body: SingleChildScrollView(
                  child: Column(
                    children: List<Widget>.generate(
                      1000,
                      (index) => Text('this is home dummy text index $index'),
                    ),
                  ),
                )),
            // When navigating to the "/second" route, build the SecondScreen widget.
            '/profile': (context) => Scaffold(
                    body: SingleChildScrollView(
                  child: Column(children: [
                    const SizedBox(
                      width: 100,
                      height: 50,
                      child: UiKitView(
                        viewType: "freezer",
                        creationParamsCodec: StandardMessageCodec(),
                      ),
                    ),
                    ...List<Widget>.generate(
                      1000,
                      (index) => Text('this is profile dummy text index $index'),
                    ),
                  ]),
                )),
          },
          debugShowCheckedModeBanner: false,
        );
      }
    }

    
</details>

### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

https://github.com/user-attachments/assets/b0ad3e28-ba93-4283-a13d-b6455508e604

</details>


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.6.1 23G93 darwin-arm64, locale en-DE)
    • Flutter version 3.24.3 on channel stable at /Users/bruno.pastre/fvm/versions/3.24.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (3 months ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/bruno.pastre/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-79915917-b509.11)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.3+-79915917-b509.11)

[✓] VS Code (version 1.95.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.102.0

[✓] Connected device (5 available)
    • Bruno’s iPhone (mobile)             • 00008120-00141D201412201E            • ios            • iOS 18.1.1 22B91
    • iPhone SE (3rd generation) (mobile) • C645237F-3C82-4732-9801-C431210747AA • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-5 (simulator)
    • macOS (desktop)                     • macos                                • darwin-arm64   • macOS 14.6.1 23G93 darwin-arm64
    • Mac Designed for iPad (desktop)     • mac-designed-for-ipad                • darwin         • macOS 14.6.1 23G93 darwin-arm64
    • Chrome (web)                        • chrome                               • web-javascript • Google Chrome 128.0.6613.120

[✓] Network resources
    • All expected network resources are available.

• No issues found!

```

</details>