Skip to content

Commit

Permalink
[camera] Fix memory leaks in example and activate leak testing (#8287)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinVignal authored Dec 28, 2024
1 parent 9dffa39 commit c515499
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT

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

## 0.11.0+2

Expand Down
16 changes: 10 additions & 6 deletions packages/camera/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
double _minAvailableExposureOffset = 0.0;
double _maxAvailableExposureOffset = 0.0;
double _currentExposureOffset = 0.0;
late AnimationController _flashModeControlRowAnimationController;
late Animation<double> _flashModeControlRowAnimation;
late AnimationController _exposureModeControlRowAnimationController;
late Animation<double> _exposureModeControlRowAnimation;
late AnimationController _focusModeControlRowAnimationController;
late Animation<double> _focusModeControlRowAnimation;
late final AnimationController _flashModeControlRowAnimationController;
late final CurvedAnimation _flashModeControlRowAnimation;
late final AnimationController _exposureModeControlRowAnimationController;
late final CurvedAnimation _exposureModeControlRowAnimation;
late final AnimationController _focusModeControlRowAnimationController;
late final CurvedAnimation _focusModeControlRowAnimation;
double _minAvailableZoom = 1.0;
double _maxAvailableZoom = 1.0;
double _currentScale = 1.0;
Expand Down Expand Up @@ -103,7 +103,11 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_flashModeControlRowAnimationController.dispose();
_flashModeControlRowAnimation.dispose();
_exposureModeControlRowAnimationController.dispose();
_exposureModeControlRowAnimation.dispose();
_focusModeControlRowAnimationController.dispose();
_focusModeControlRowAnimation.dispose();
super.dispose();
}

Expand Down
1 change: 1 addition & 0 deletions packages/camera/camera/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter
leak_tracker_flutter_testing: any

flutter:
uses-material-design: true
13 changes: 13 additions & 0 deletions packages/camera/camera/example/test/flutter_test_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

Future<void> testExecutable(FutureOr<void> Function() testMain) async {
LeakTesting.enable();
LeakTracking.warnForUnsupportedPlatforms = false;
await testMain();
}
1 change: 1 addition & 0 deletions packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
leak_tracker_flutter_testing: any
mockito: ^5.4.4
plugin_platform_interface: ^2.1.7

Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera/test/camera_preview_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void main() {
debugDefaultTargetPlatformOverride = TargetPlatform.android;

final FakeController controller = FakeController();
addTearDown(controller.dispose);
controller.value = controller.value.copyWith(
isInitialized: true,
isRecordingVideo: true,
Expand Down Expand Up @@ -179,6 +180,7 @@ void main() {
debugDefaultTargetPlatformOverride = TargetPlatform.android;

final FakeController controller = FakeController();
addTearDown(controller.dispose);
controller.value = controller.value.copyWith(
isInitialized: true,
deviceOrientation: DeviceOrientation.portraitUp,
Expand Down Expand Up @@ -213,6 +215,7 @@ void main() {
debugDefaultTargetPlatformOverride = TargetPlatform.android;

final FakeController controller = FakeController();
addTearDown(controller.dispose);
controller.value = controller.value.copyWith(
isInitialized: true,
deviceOrientation: DeviceOrientation.portraitUp,
Expand Down Expand Up @@ -241,6 +244,7 @@ void main() {
(WidgetTester tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
final FakeController controller = FakeController();
addTearDown(controller.dispose);
controller.value = controller.value.copyWith(
isInitialized: true,
previewSize: const Size(480, 640),
Expand Down
13 changes: 13 additions & 0 deletions packages/camera/camera/test/flutter_test_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

Future<void> testExecutable(FutureOr<void> Function() testMain) async {
LeakTesting.enable();
LeakTracking.warnForUnsupportedPlatforms = false;
await testMain();
}

0 comments on commit c515499

Please sign in to comment.