Skip to content

Commit c515499

Browse files
[camera] Fix memory leaks in example and activate leak testing (#8287)
1 parent 9dffa39 commit c515499

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

packages/camera/camera/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## NEXT
22

33
* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.
4+
* Updates example to dispose animation controllers and curved animations.
45

56
## 0.11.0+2
67

packages/camera/camera/example/lib/main.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
5454
double _minAvailableExposureOffset = 0.0;
5555
double _maxAvailableExposureOffset = 0.0;
5656
double _currentExposureOffset = 0.0;
57-
late AnimationController _flashModeControlRowAnimationController;
58-
late Animation<double> _flashModeControlRowAnimation;
59-
late AnimationController _exposureModeControlRowAnimationController;
60-
late Animation<double> _exposureModeControlRowAnimation;
61-
late AnimationController _focusModeControlRowAnimationController;
62-
late Animation<double> _focusModeControlRowAnimation;
57+
late final AnimationController _flashModeControlRowAnimationController;
58+
late final CurvedAnimation _flashModeControlRowAnimation;
59+
late final AnimationController _exposureModeControlRowAnimationController;
60+
late final CurvedAnimation _exposureModeControlRowAnimation;
61+
late final AnimationController _focusModeControlRowAnimationController;
62+
late final CurvedAnimation _focusModeControlRowAnimation;
6363
double _minAvailableZoom = 1.0;
6464
double _maxAvailableZoom = 1.0;
6565
double _currentScale = 1.0;
@@ -103,7 +103,11 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
103103
void dispose() {
104104
WidgetsBinding.instance.removeObserver(this);
105105
_flashModeControlRowAnimationController.dispose();
106+
_flashModeControlRowAnimation.dispose();
106107
_exposureModeControlRowAnimationController.dispose();
108+
_exposureModeControlRowAnimation.dispose();
109+
_focusModeControlRowAnimationController.dispose();
110+
_focusModeControlRowAnimation.dispose();
107111
super.dispose();
108112
}
109113

packages/camera/camera/example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dev_dependencies:
2727
sdk: flutter
2828
integration_test:
2929
sdk: flutter
30+
leak_tracker_flutter_testing: any
3031

3132
flutter:
3233
uses-material-design: true
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8+
9+
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
10+
LeakTesting.enable();
11+
LeakTracking.warnForUnsupportedPlatforms = false;
12+
await testMain();
13+
}

packages/camera/camera/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies:
3232
dev_dependencies:
3333
flutter_test:
3434
sdk: flutter
35+
leak_tracker_flutter_testing: any
3536
mockito: ^5.4.4
3637
plugin_platform_interface: ^2.1.7
3738

packages/camera/camera/test/camera_preview_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ void main() {
144144
debugDefaultTargetPlatformOverride = TargetPlatform.android;
145145

146146
final FakeController controller = FakeController();
147+
addTearDown(controller.dispose);
147148
controller.value = controller.value.copyWith(
148149
isInitialized: true,
149150
isRecordingVideo: true,
@@ -179,6 +180,7 @@ void main() {
179180
debugDefaultTargetPlatformOverride = TargetPlatform.android;
180181

181182
final FakeController controller = FakeController();
183+
addTearDown(controller.dispose);
182184
controller.value = controller.value.copyWith(
183185
isInitialized: true,
184186
deviceOrientation: DeviceOrientation.portraitUp,
@@ -213,6 +215,7 @@ void main() {
213215
debugDefaultTargetPlatformOverride = TargetPlatform.android;
214216

215217
final FakeController controller = FakeController();
218+
addTearDown(controller.dispose);
216219
controller.value = controller.value.copyWith(
217220
isInitialized: true,
218221
deviceOrientation: DeviceOrientation.portraitUp,
@@ -241,6 +244,7 @@ void main() {
241244
(WidgetTester tester) async {
242245
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
243246
final FakeController controller = FakeController();
247+
addTearDown(controller.dispose);
244248
controller.value = controller.value.copyWith(
245249
isInitialized: true,
246250
previewSize: const Size(480, 640),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8+
9+
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
10+
LeakTesting.enable();
11+
LeakTracking.warnForUnsupportedPlatforms = false;
12+
await testMain();
13+
}

0 commit comments

Comments
 (0)