Repository navigation
[camera_avfoundation] Leaking sampleBuffer in captureOutput #132013
Description
Is there an existing issue for this?
- I have searched the existing issues
- I have read the guide to filing a bug
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
[-][camera_avfoundation] Memory leak in of sampleBuffer in captureOutput[/-][+][camera_avfoundation] Memory leak of sampleBuffer in captureOutput[/+][-][camera_avfoundation] Memory leak of sampleBuffer in captureOutput[/-][+][camera_avfoundation] Leaking sampleBuffer in captureOutput[/+]danagbemava-nc commentedon Aug 7, 2023
Reproducible using the code sample provided above.
flutter doctor -v
flutter-triage-bot commentedon Dec 25, 2023
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!
[camera]fix a sample buffer memory leak on pause resume recording (#5927
github-actions commentedon Feb 19, 2024
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 -vand a minimal reproduction of the issue.