Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[camera] Fix memory leaks in example and activate leak testing #8287

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use any rather than ^3.0.9?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what is recommended in the documentation (https://github.com/dart-lang/leak_tracker/blob/main/doc%2Fleak_tracking%2FDETECT.md) because the version is defined by the Flutter SDK.


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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this is ran. Does this run automatically with flutter test? Could you add a comment that explains how this is used or provide a doc link?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is run automatically with flutter test. Here is the documentation https://api.flutter.dev/flutter/flutter_test/flutter_test-library.html

I don't mind adding a comment or something if you feel it is needed. Could you tell me what kind of comment you are expecting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. Sorry I was unfamiliar with this in flutter_test. As long as there is public documentation for this then it doesn't need a comment.

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();
}
Loading