Skip to content

Commit 0625df0

Browse files
committed
Securing against memory leaks 2
1 parent 4db4481 commit 0625df0

File tree

2 files changed

+65
-50
lines changed

2 files changed

+65
-50
lines changed

src/Core/src/Handlers/DatePicker/DatePickerHandler.Android.cs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace Microsoft.Maui.Handlers
88
public partial class DatePickerHandler : ViewHandler<IDatePicker, MauiDatePicker>
99
{
1010
DatePickerDialog? _dialog;
11-
EventHandler? _setDateLater;
12-
EventHandler? _dismiss;
11+
(int year, int month, int day) _selectedDate;
1312

1413
protected override MauiDatePicker CreatePlatformView()
1514
{
@@ -54,8 +53,11 @@ protected override void DisconnectHandler(MauiDatePicker platformView)
5453
{
5554
if (_dialog != null)
5655
{
57-
_dialog.DismissEvent -= _dismiss;
58-
_dialog.ShowEvent -= _setDateLater;
56+
_dialog.DismissEvent -= OnDismissEvent;
57+
_dialog.ShowEvent -= OnShowEvent;
58+
if (OperatingSystem.IsAndroidVersionAtLeast(24))
59+
_dialog.DateSet -= OnDateSet;
60+
5961
_dialog.Hide();
6062
_dialog.Dispose();
6163
_dialog = null;
@@ -70,17 +72,7 @@ protected override void DisconnectHandler(MauiDatePicker platformView)
7072

7173
protected virtual DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
7274
{
73-
void OnDateSelectedCallback(object? obj, DatePickerDialog.DateSetEventArgs e)
74-
{
75-
if (VirtualView != null)
76-
{
77-
VirtualView.Date = e.Date;
78-
VirtualView.IsFocused = false;
79-
}
80-
}
81-
82-
var dialog = new DatePickerDialog(Context!, OnDateSelectedCallback, year, month, day);
83-
75+
var dialog = new DatePickerDialog(Context!, OnDateSet, year, month, day);
8476
return dialog;
8577
}
8678

@@ -148,22 +140,42 @@ void ShowPickerDialog(int year, int month, int day)
148140
_dialog = CreateDatePickerDialog(year, month, day);
149141
else
150142
{
151-
_setDateLater = (sender, e) => { _dialog!.UpdateDate(year, month, day); _dialog.ShowEvent -= _setDateLater; };
152-
_dialog.ShowEvent += _setDateLater;
153-
_dismiss = (sender, e) =>
154-
{
155-
if (VirtualView != null)
156-
{
157-
VirtualView.IsFocused = false;
158-
}
159-
_dialog.DismissEvent -= _dismiss;
160-
};
161-
_dialog.DismissEvent += _dismiss;
143+
_selectedDate = (year, month, day);
144+
_dialog.ShowEvent += OnShowEvent;
145+
_dialog.DismissEvent += OnDismissEvent;
162146
}
163147

164148
_dialog.Show();
165149
}
166150

151+
void OnDismissEvent(object? sender, EventArgs e)
152+
{
153+
if (VirtualView != null)
154+
{
155+
VirtualView.IsFocused = false;
156+
}
157+
158+
if (_dialog != null)
159+
{
160+
_dialog.DismissEvent -= OnDismissEvent;
161+
}
162+
}
163+
164+
void OnShowEvent(object? sender, EventArgs e)
165+
{
166+
_dialog!.UpdateDate(_selectedDate.year, _selectedDate.month, _selectedDate.day);
167+
_dialog.ShowEvent -= OnShowEvent;
168+
}
169+
170+
void OnDateSet(object? obj, DatePickerDialog.DateSetEventArgs e)
171+
{
172+
if (VirtualView != null)
173+
{
174+
VirtualView.Date = e.Date;
175+
VirtualView.IsFocused = false;
176+
}
177+
}
178+
167179
void HidePickerDialog()
168180
{
169181
_dialog?.Hide();

src/Core/src/Handlers/TimePicker/TimePickerHandler.Android.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public partial class TimePickerHandler : ViewHandler<ITimePicker, MauiTimePicker
1010
{
1111
MauiTimePicker? _timePicker;
1212
TimePickerDialog? _dialog;
13-
EventHandler? dismiss;
1413

1514
protected override MauiTimePicker CreatePlatformView()
1615
{
@@ -28,41 +27,45 @@ protected override void DisconnectHandler(MauiTimePicker platformView)
2827
if (_dialog != null)
2928
{
3029
_dialog.Hide();
31-
_dialog.DismissEvent -= dismiss;
30+
_dialog.DismissEvent -= OnDismiss;
3231
_dialog = null;
3332
}
3433
}
3534

3635
protected virtual TimePickerDialog CreateTimePickerDialog(int hour, int minute)
3736
{
38-
void onTimeSetCallback(object? obj, TimePickerDialog.TimeSetEventArgs args)
39-
{
40-
if (VirtualView == null || PlatformView == null)
41-
return;
37+
var dialog = new TimePickerDialog(Context!, OnTimeSetCallback, hour, minute, Use24HourView);
38+
dialog.DismissEvent += OnDismiss;
4239

43-
VirtualView.Time = new TimeSpan(args.HourOfDay, args.Minute, 0);
44-
VirtualView.IsFocused = false;
40+
return dialog;
41+
}
4542

46-
if (_dialog != null)
47-
{
48-
_dialog.DismissEvent -= dismiss;
49-
_dialog = null;
50-
}
51-
}
43+
private void OnTimeSetCallback(object? obj, TimePickerDialog.TimeSetEventArgs args)
44+
{
45+
if (VirtualView == null || PlatformView == null)
46+
return;
5247

53-
var dialog = new TimePickerDialog(Context!, onTimeSetCallback, hour, minute, Use24HourView);
48+
VirtualView.Time = new TimeSpan(args.HourOfDay, args.Minute, 0);
49+
VirtualView.IsFocused = false;
5450

55-
dismiss = (sender, e) =>
51+
if (_dialog != null)
5652
{
57-
if (VirtualView != null)
58-
{
59-
VirtualView.IsFocused = false;
60-
}
61-
dialog.DismissEvent -= dismiss;
62-
};
63-
dialog.DismissEvent += dismiss;
53+
_dialog.DismissEvent -= OnDismiss;
54+
_dialog = null;
55+
}
56+
}
6457

65-
return dialog;
58+
private void OnDismiss(object? sender, EventArgs e)
59+
{
60+
if (VirtualView != null)
61+
{
62+
VirtualView.IsFocused = false;
63+
}
64+
65+
if (_dialog != null)
66+
{
67+
_dialog.DismissEvent -= OnDismiss;
68+
}
6669
}
6770

6871
// This is a Android-specific mapping

0 commit comments

Comments
 (0)