Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repeat drawImage result of toImageSync leading memory leak #148579

Closed
leegean opened this issue May 17, 2024 · 3 comments
Closed

repeat drawImage result of toImageSync leading memory leak #148579

leegean opened this issue May 17, 2024 · 3 comments
Labels
r: duplicate Issue is closed as a duplicate of an existing issue

Comments

@leegean
Copy link

leegean commented May 17, 2024

Steps to reproduce

1.execute program.
2.waiting and observe memory.
3.press mouse left to stop memory increasing.

Expected results

memory is released.

Actual results

memory remain unchanged.

Code sample

Code sample
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

// The FlutterView into which this example will draw; set in the main method.
late final ui.FlutterView view;

void paintExpensive(ui.Canvas canvas, ui.Rect paintBounds) {
}

void paintCheap(ui.Canvas canvas, ui.Rect paintBounds) {
}

ui.Image? lastImage;

bool stop=false;

ui.Picture paint(ui.Rect paintBounds) {
  final ui.PictureRecorder recorder = ui.PictureRecorder();
  final ui.Canvas canvas = ui.Canvas(recorder, paintBounds);

  ui.Picture picture;

  if (lastImage == null) {
    paintExpensive(canvas, paintBounds);
    picture = recorder.endRecording();

    lastImage = picture.toImageSync(paintBounds.width.ceil(), paintBounds.height.ceil());
  } else {
 
     canvas.drawImage(lastImage!, ui.Offset.zero, ui.Paint());
    paintCheap(canvas, paintBounds);
    picture = recorder.endRecording();

    if(!stop){
      lastImage?.dispose();
      lastImage = picture.toImageSync(paintBounds.width.ceil(), paintBounds.height.ceil());
    }

  }

  return picture;
}

ui.Scene composite(ui.Picture picture, ui.Rect paintBounds) {
  double devicePixelRatio = view.devicePixelRatio;
  final Float64List deviceTransform = Float64List(16)
    ..[0] = devicePixelRatio
    ..[5] = devicePixelRatio
    ..[10] = 1.0
    ..[15] = 1.0;

  final ui.SceneBuilder sceneBuilder = ui.SceneBuilder()
    ..pushTransform(deviceTransform)
    ..addPicture(ui.Offset.zero, picture)
    ..pop();
  return sceneBuilder.build();
}

Future<void> beginFrame(Duration timeStamp) async {
  final ui.Rect paintBounds = ui.Offset.zero & (view.physicalSize / view.devicePixelRatio);
  final picture = paint(paintBounds);

  final ui.Scene scene = composite(picture, paintBounds);
picture.dispose();
  view.render(scene);
  ui.PlatformDispatcher.instance.scheduleFrame();
}
void handlePointerDataPacket(ui.PointerDataPacket packet) {
  var expand = PointerEventConverter.expand(packet.data, (viewId) => view.devicePixelRatio).toList();
  if(expand.isEmpty)return;
  PointerEvent? lastEvent = expand.last;

  if(lastEvent is PointerDownEvent){
    if(lastEvent.buttons==kPrimaryMouseButton){
      stop=true;  //this will prevent memory incresing.
    }
  
  }
}

void main() {

  view = ui.PlatformDispatcher.instance.implicitView!;
  // Finally, we register our beginFrame callback and kick off the first frame.
  ui.PlatformDispatcher.instance
    ..onBeginFrame = beginFrame
    ..onPointerDataPacket = handlePointerDataPacket
    ..scheduleFrame();
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.19.4, on Microsoft Windows [版本 10.0.19045.4412], locale zh-CN)
    • Flutter version 3.19.4 on channel stable at D:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 68bfaea224 (8 weeks ago), 2024-03-20 15:36:31 -0700
    • Engine revision a5c24f538d
    • Dart version 3.3.2
    • DevTools version 2.31.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at D:\Android\Sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = D:\Android\Sdk
    • Java binary at: D:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0--11572160)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.3)
    • Visual Studio at D:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.8.34330.188
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2023.3)
    • Android Studio at D:\Program Files\Android\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.10+0--11572160)

[√] IntelliJ IDEA Community Edition (version 2024.1)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.1
    • Flutter plugin version 79.1.3
    • Dart plugin version 241.15845

[√] IntelliJ IDEA Ultimate Edition (version 2022.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2022.3
    • Flutter plugin version 72.1.4
    • Dart plugin version 223.7571.203

[√] VS Code (version 1.76.2)
    • VS Code at C:\Users\Administrator\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.86.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [版本 10.0.19045.4412]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 125.0.6422.60
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 124.0.2478.105

[√] Network resources
    • All expected network resources are available.

• No issues found!
@alexgarbarev
Copy link

Hi @leegean, I think you forgot to dispose a picture created there: picture = recorder.endRecording();.

@leegean
Copy link
Author

leegean commented May 18, 2024

Hi @leegean, I think you forgot to dispose a picture created there: picture = recorder.endRecording();.

i's not working.besides,addPicture need it.

@leegean leegean closed this as completed May 19, 2024
@leegean leegean reopened this May 19, 2024
@huycozy huycozy added the in triage Presently being triaged by the triage team label May 20, 2024
@huycozy
Copy link
Member

huycozy commented May 20, 2024

@leegean I see there's a similar issue with toImageSync at #138627 (another duplicated issue #145210).

Closing this issue in favor of that one, so please follow up on #138627 for further updates. Thank you!

@huycozy huycozy closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
@huycozy huycozy added r: duplicate Issue is closed as a duplicate of an existing issue and removed in triage Presently being triaged by the triage team labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
r: duplicate Issue is closed as a duplicate of an existing issue
Projects
None yet
Development

No branches or pull requests

3 participants