Skip to content

[camera_avfoundation] Leaking sampleBuffer in captureOutput #132013

Closed

Description

@misos1

Is there an existing issue for this?

Steps to reproduce

Run the code sample and wait.

Expected results

No memory leak.

Actual results

CameraPreview stops after a while.

There is memory leak in: https://github.com/flutter/packages/blob/d7ee75ad59ad7bc45e659d0599e935e9e7981ea1/packages/camera/camera_avfoundation/ios/Classes/FLTCam.m#L503-L560

There are two lines with return: https://github.com/flutter/packages/blob/d7ee75ad59ad7bc45e659d0599e935e9e7981ea1/packages/camera/camera_avfoundation/ios/Classes/FLTCam.m#L522
https://github.com/flutter/packages/blob/d7ee75ad59ad7bc45e659d0599e935e9e7981ea1/packages/camera/camera_avfoundation/ios/Classes/FLTCam.m#L547

which does not release sampleBuffer.

Actually it is needless to retain sampleBuffer here at all because it is owned by the caller of captureOutput. I suppose the original author put this there because the replacement sample buffer from adjustTime needs to be released so this way is not needed to check whether the sample buffer is original or goes from adjustTime when called CFRelease.

This sample code may occasionally trigger another bugs like #69874 with:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:] A pixel buffer cannot be appended when readyForMoreMediaData is NO.'
libc++abi: terminating with uncaught exception of type NSException
* thread #5, queue = 'io.flutter.camera.captureSessionQueue', stop reason = signal SIGABRT

Or an exception having something to do with uninitialised _lastVideoSampleTime.

Code sample

Code sample
import "dart:io";
import "package:flutter/material.dart";
import "package:camera/camera.dart";

void main() {
	runApp(const MyApp());
}

class MyApp extends StatelessWidget {
	const MyApp({super.key});
	@override Widget build(BuildContext context) => const MaterialApp(home: MyHomePage());
}

class MyHomePage extends StatefulWidget {
	const MyHomePage({super.key});
	@override State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
	bool loading = true;
	bool recording = true;
	late CameraController camera_cont;

	@override void dispose() {
		camera_cont.dispose();
		super.dispose();
	}

	@override void initState() {
		super.initState();
		init_camera();
	}

	init_camera() async {
		final cameras = await availableCameras();
		camera_cont = CameraController(cameras.firstWhere((camera) => camera.lensDirection == CameraLensDirection.back), ResolutionPreset.ultraHigh);
		await camera_cont.initialize();
		setState(() => loading = false);
		await camera_cont.prepareForVideoRecording();
		await camera_cont.startVideoRecording();
		for(int i = 0; i < 100; i++)
		{
			await Future.delayed(const Duration(milliseconds: 100));
			await camera_cont.pauseVideoRecording();
			setState(() => recording = false);
			await Future.delayed(const Duration(milliseconds: 100));
			await camera_cont.resumeVideoRecording();
			setState(() => recording = true);
		}
	}

	@override Widget build(BuildContext context)
	{
		if(loading)return const Center(child: CircularProgressIndicator());
		else return Stack(
			alignment: Alignment.center,
			children: [
				CameraPreview(camera_cont),
				Icon(recording ? Icons.play_circle : Icons.pause_circle),
			],
		);
	}
}

Screenshots or Video

Screenshots / Video demonstration
Replay.mov

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.10.6, on macOS 13.5 22G74 darwin-x64)

Activity

  1. changed the title [-][camera_avfoundation] Memory leak in of sampleBuffer in captureOutput[/-] [+][camera_avfoundation] Memory leak of sampleBuffer in captureOutput[/+] on Aug 6, 2023
  2. changed the title [-][camera_avfoundation] Memory leak of sampleBuffer in captureOutput[/-] [+][camera_avfoundation] Leaking sampleBuffer in captureOutput[/+] on Aug 6, 2023
  3. danagbemava-nc commented on Aug 7, 2023

    @danagbemava-nc
    Member

    Reproducible using the code sample provided above.

    flutter doctor -v
    [!] Flutter (Channel stable, 3.10.6, on macOS 13.4.1 22F770820d darwin-arm64, locale en-GB)
        • Flutter version 3.10.6 on channel stable at /Users/nexus/dev/sdks/flutter
        ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter_beta/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutter. Consider adding /Users/nexus/dev/sdks/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter_beta/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutter. Consider adding /Users/nexus/dev/sdks/flutter/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision f468f3366c (4 weeks ago), 2023-07-12 15:19:05 -0700
        • Engine revision cdbeda788a
        • Dart version 3.0.6
        • DevTools version 2.23.1
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
        • Android SDK at /Users/nexus/Library/Android/sdk
        • Platform android-33, build-tools 33.0.1
        • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/223.8836.35.2231.10406996/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3)
        • Xcode at /Applications/Xcode-14.3.0.app/Contents/Developer
        • Build 14E222b
        • CocoaPods version 1.12.1
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.2)
        • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/222.4459.24.2221.10121639/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 17.0.6+0-17.0.6b802.4-9586694)
    
    [✓] Android Studio (version 2022.3)
        • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/223.8836.35.2231.10406996/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 17.0.6+0-17.0.6b829.9-10027231)
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2023.2)
        • IntelliJ at /Users/nexus/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
        • Flutter plugin version 75.1.4
        • Dart plugin version 232.8660.129
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2023.1.4)
        • IntelliJ at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/231.9225.16/IntelliJ IDEA.app
        • 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
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2023.2)
        • IntelliJ at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/232.8660.185/IntelliJ IDEA.app
        • 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
    
    [✓] VS Code (version 1.81.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.70.0
    
    [✓] Connected device (3 available)
        • Nexus (mobile)  • 00008020-001875E83A38002E • ios            • iOS 16.6 20G75
        • macOS (desktop) • macos                     • darwin-arm64   • macOS 13.4.1 22F770820d darwin-arm64
        • Chrome (web)    • chrome                    • web-javascript • Google Chrome 115.0.5790.114
        ! Error: Nexus is busy: Fetching debug symbols for Nexus. Xcode will continue when Nexus is finished. (code -10)
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.13.0-17.0.pre.76, on macOS 13.4.1 22F770820d darwin-arm64, locale en-GB)
        • Flutter version 3.13.0-17.0.pre.76 on channel master at /Users/nexus/dev/sdks/flutters
        ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter_beta/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter_beta/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision bfbe79feb2 (7 hours ago), 2023-08-06 21:47:34 -0400
        • Engine revision 15b5707af4
        • Dart version 3.2.0 (build 3.2.0-41.0.dev)
        • DevTools version 2.26.1
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
        • Android SDK at /Users/nexus/Library/Android/sdk
        • Platform android-33, build-tools 33.0.1
        • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/223.8836.35.2231.10406996/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3)
        • Xcode at /Applications/Xcode-14.3.0.app/Contents/Developer
        • Build 14E222b
        • CocoaPods version 1.12.1
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.2)
        • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/222.4459.24.2221.10121639/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 17.0.6+0-17.0.6b802.4-9586694)
    
    [✓] Android Studio (version 2022.3)
        • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/223.8836.35.2231.10406996/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 17.0.6+0-17.0.6b829.9-10027231)
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2023.1.4)
        • IntelliJ at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/231.9225.16/IntelliJ IDEA.app
        • 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
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2023.2)
        • IntelliJ at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/232.8660.185/IntelliJ IDEA.app
        • 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
    
    [✓] VS Code (version 1.81.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.70.0
    
    [✓] Connected device (3 available)
        • Nexus (mobile)  • 00008020-001875E83A38002E • ios            • iOS 16.6 20G75
        • macOS (desktop) • macos                     • darwin-arm64   • macOS 13.4.1 22F770820d darwin-arm64
        • Chrome (web)    • chrome                    • web-javascript • Google Chrome 115.0.5790.114
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
  4. added
    platform-iosiOS applications specifically
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    fyi-ecosystemFor the attention of Ecosystem team
    team-iosOwned by iOS platform team
    packageflutter/packages repository. See also p: labels.
    and removed
    in triagePresently being triaged by the triage team
    on Aug 7, 2023
  5. added
    P1High-priority issues at the top of the work list
    triaged-iosTriaged by iOS platform team
    on Aug 7, 2023
  6. flutter-triage-bot commented on Dec 25, 2023

    @flutter-triage-bot

    This issue is assigned to @hellohuanlin but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  7. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Dec 25, 2023
  8. added
    r: fixedIssue is closed as already fixed in a newer version
    on Feb 5, 2024
  9. github-actions commented on Feb 19, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

⏳Bot is counting down the days until it unassigns the issueP1High-priority issues at the top of the work listfound in release: 3.10Found to occur in 3.10found in release: 3.13Found to occur in 3.13fyi-ecosystemFor the attention of Ecosystem teamhas reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-iosOwned by iOS platform teamtriaged-iosTriaged by iOS platform team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions