Repository: flutter
Issue: Application stopping and/or crashing when painting last Picture to canvas
Labels: c: new feature,c: crash,engine,has reproducible steps,P3,c: fatal crash,found in release: 3.10,found in release: 3.11,team-engine,triaged-engine

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

1. Switch to flutter channel master
2. Create new application using PlatformDispatcher methods to render to flutter
3. Cache last painted Picture to a local variable
4. On next frame loop paint the last picture behind everything else
5. After some loops the app will become unresponsive and eventually crash

### Expected results

The app should continue to run properly or at least be explicit about the error

### Actual results

The application will hang and after a while it will crash (or just hang with an error in the case of web). With VM no error is reported, even with verbose mode

### Code sample

<details><summary>Code sample</summary>

```dart
import 'dart:ui';

late final FlutterView view;

void main() {
  assert(PlatformDispatcher.instance.implicitView != null);
  view = PlatformDispatcher.instance.implicitView!;

  PlatformDispatcher.instance
    ..onBeginFrame = _draw
    ..scheduleFrame();
}

Picture? _lastPicture;
void _draw(Duration duration) {
  final recorder = PictureRecorder();
  final canvas = Canvas(recorder);
  
  if(_lastPicture != null) {
    canvas.drawPicture(_lastPicture!);
    _lastPicture?.dispose();
    _lastPicture = null;
  }

  canvas.drawPaint(Paint()..color = const Color(0xFF000000));
  canvas.drawLine(
    const Offset(0, 0),
    Offset(view.physicalSize.width, view.physicalSize.height * (frameCount % 60 / 60)),
    Paint()..color = const Color(0xFFFF0000),
  );
  
  final picture = recorder.endRecording();
  final scene = SceneBuilder()..addPicture(Offset.zero, picture);
  _lastPicture = picture;
  
  view.render(scene.build());
  frameCount++;
  PlatformDispatcher.instance.scheduleFrame();
}

int frameCount = 0;
```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs</summary>
On VM there are none, web outputs a single error and then dies
```console
Uncaught RuntimeError: memory access out of bounds
    at canvaskit.wasm:0xd354
    at canvaskit.wasm:0x803a
    at canvaskit.wasm:0x71dd3
    at canvaskit.wasm:0x4383a7
    at canvaskit.wasm:0x69e26
    at Tb.<anonymous> (canvaskit.js:117:52)
    at Tb.<anonymous> (canvaskit.js:156:261)
    at new PictureRecorder (canvaskit.js:100:258)
    at SkPictureRecorder.new (canvaskit_api.dart:2046:25)
    at _engine.CkPictureRecorder.new.beginRecording (picture_recorder.dart:16:54)
    at CanvasKitCanvas.new (canvaskit_canvas.dart:18:40)
    at _engine.CanvasKitRenderer.new.createCanvas (renderer.dart:99:5)
    at Canvas.new (canvas.dart:64:21)
    at _draw (<anonymous>:1930:31)
    at Object.invoke1 (platform_dispatcher.dart:1257:13)
    at _engine.EnginePlatformDispatcher.new.invokeOnBeginFrame (platform_dispatcher.dart:223:5)
    at initialization.dart:180:45
    at Object._checkAndCall (operations.dart:504:37)
    at Object.dcall (operations.dart:509:39)
    at ret (js_allow_interop_patch.dart:17:11)
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[√] Flutter (Channel master, 3.11.0-6.0.pre.78, on Microsoft Windows [Versione 10.0.25357.1], locale it-IT)
    • Flutter version 3.11.0-6.0.pre.78 on channel master at C:\Users\dnbia\Repos\flutter
    • Upstream repository https://github.com/flutter/flutter
    • Framework revision 76a9f68325 (24 hours ago), 2023-05-15 14:17:11 -0500
    • Engine revision dffc480a2b
    • Dart version 3.1.0 (build 3.1.0-112.0.dev)
    • DevTools version 2.23.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at C:\Users\dnbia\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2022 17.3.6)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools
    • Visual Studio Build Tools 2022 version 17.3.32929.385
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2023.1)
    • Android Studio at C:\Program Files\Android Studio
    • 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 17.0.6+0-b2043.56-9884540)

[√] VS Code (version 1.77.3)
    • VS Code at C:\Users\dnbia\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.64.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Versione 10.0.25357.1]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 113.0.5672.93
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 113.0.1774.35

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
```

</details>