Skip to content

Commit 5866320

Browse files
Resolved memory leak in OTP input and Tab view
1 parent 0176192 commit 5866320

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

maui/src/OtpInput/SfOtpInput.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public class SfOtpInput : SfView, IKeyboardListener, IParentThemeElement
128128
/// Represents the previous value of the OTP input.
129129
/// </summary>
130130
string? _oldText;
131+
132+
/// <summary>
133+
/// Stores the last UITextField to unhook event handlers.
134+
/// </summary>
135+
UIKit.UITextField? _lastPlatformView;
131136
#endif
132137

133138
/// <summary>
@@ -2241,6 +2246,15 @@ void OnHandlerChanged(object? sender, EventArgs e)
22412246
{
22422247
if (sender is OTPEntry textBox)
22432248
{
2249+
#if MACCATALYST || IOS
2250+
// Unhook from previous handler if exists
2251+
if (_lastPlatformView is not null)
2252+
{
2253+
_lastPlatformView.ShouldChangeCharacters -= ValidateText;
2254+
_lastPlatformView = null;
2255+
}
2256+
#endif
2257+
22442258
#if WINDOWS
22452259
if ((sender as OTPEntry)?.Handler is not null && (sender as OTPEntry)?.Handler?.PlatformView is Microsoft.UI.Xaml.Controls.TextBox platformView)
22462260
{
@@ -2256,10 +2270,11 @@ void OnHandlerChanged(object? sender, EventArgs e)
22562270
}
22572271

22582272
#elif MACCATALYST || IOS
2259-
if ((sender as OTPEntry)?.Handler is not null && (sender as OTPEntry)?.Handler?.PlatformView is UIKit.UITextField platformView)
2273+
if ((sender as OTPEntry)?.Handler is not null && (sender as OTPEntry)?.Handler?.PlatformView is UIKit.UITextField platformView)
22602274
{
22612275
platformView.ShouldChangeCharacters += ValidateText;
2262-
}
2276+
_lastPlatformView = platformView;
2277+
}
22632278
#endif
22642279
}
22652280
}

maui/src/TabView/Control/SfTabBar.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,12 +3213,19 @@ protected override Size MeasureOverride(double widthConstraint, double heightCon
32133213
/// </summary>
32143214
protected override void OnHandlerChanged()
32153215
{
3216-
base.OnHandlerChanged();
3217-
if (Handler == null)
3218-
{
3219-
Children.Clear();
3220-
}
3221-
}
3216+
if (Handler == null)
3217+
{
3218+
if (Items != null)
3219+
{
3220+
foreach (var item in Items)
3221+
{
3222+
item.Touched -= OnTabItemTouched;
3223+
}
3224+
}
3225+
}
3226+
3227+
base.OnHandlerChanged();
3228+
}
32223229

32233230
#endregion
32243231

0 commit comments

Comments
 (0)