v2.42.1 — HIGH: Timer-Periodic-Leak behoben

- MeloApp von StatelessWidget zu StatefulWidget konvertiert
- _autoScanTimer als globale Variable gespeichert
- Timer wird in _MeloAppState.dispose() gecancelt
This commit is contained in:
Dustin
2026-08-02 17:00:23 +02:00
parent a483436490
commit b5b1475d5d
+19 -4
View File
@@ -21,6 +21,9 @@ import 'screens/login_screen.dart';
final FlutterLocalNotificationsPlugin notificationsPlugin =
FlutterLocalNotificationsPlugin();
/// Globaler Timer für periodischen Auto-Scan (wird in MeloAppState.dispose gecancelt)
Timer? _autoScanTimer;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
@@ -96,7 +99,7 @@ void main() async {
runApp(const MeloApp());
}
/// Führt Musik-Scan beim Start aus + Timer.periodic alle 3 Stunden (Issue #6)
/// Führt Musik-Scan beim Start aus + startet Timer.periodic alle 3 Stunden (Issue #6)
void _starteAutoScan() {
// Sofort beim Start scannen (asynchron, blockiert nicht)
MusikScanner().scanneMusikOrdner().then((songs) {
@@ -105,8 +108,8 @@ void _starteAutoScan() {
MeloLogger().fehler('auto_scan_start', e);
});
// Periodischer Scan alle 3 Stunden
Timer.periodic(const Duration(hours: 3), (_) {
// Periodischer Scan alle 3 Stunden Timer wird in MeloAppState.dispose() gecancelt
_autoScanTimer = Timer.periodic(const Duration(hours: 3), (_) {
MusikScanner().scanneMusikOrdner().then((songs) {
MeloLogger().zustand('auto_scan_periodisch', {'gefunden': songs.length});
}).catchError((e) {
@@ -172,9 +175,14 @@ Future<bool> _onServiceStart(ServiceInstance service) async {
return true;
}
class MeloApp extends StatelessWidget {
class MeloApp extends StatefulWidget {
const MeloApp({super.key});
@override
State<MeloApp> createState() => _MeloAppState();
}
class _MeloAppState extends State<MeloApp> {
@override
Widget build(BuildContext context) {
final auth = AuthService();
@@ -188,4 +196,11 @@ class MeloApp extends StatelessWidget {
: const LoginScreen(),
);
}
@override
void dispose() {
_autoScanTimer?.cancel();
_autoScanTimer = null;
super.dispose();
}
}