Skip to content

[camera_avfoundation] Leaking sampleBuffer in captureOutput #132013

@misos1

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)

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