-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ios] fix memory leak in WindowOverlay (#16700)
Context: #16346 This addresses the memory leak discovered by: src/Core/src/WindowOverlay/WindowOverlay.iOS.cs(92,40): error MA0001: Event 'OnTouch' could cause memory leaks in an NSObject subclass. Remove the event or add the [UnconditionalSuppressMessage("Memory", "MA0001")] attribute with a justification as to why the event will not leak. I could reproduce a leak by writing a custom test. Solved the problem by: * Removed the `PassthroughView.OnTouch` event completely. We could just call `WindowOverlay.OnTappedInternal()` directly instead of going through an intermediate event. * `WindowOverlay overlay` is now a `WeakReference<WindowOverlay>`.
- Loading branch information
1 parent
a7e99ae
commit ff02f57
Showing
3 changed files
with
45 additions
and
17 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/Controls/tests/DeviceTests/Elements/Window/WindowOverlayTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Maui.Controls; | ||
using Microsoft.Maui.DeviceTests.Stubs; | ||
using Xunit; | ||
|
||
namespace Microsoft.Maui.DeviceTests.Memory; | ||
|
||
[Category(TestCategory.WindowOverlay)] | ||
public class WindowOverlayTests : ControlsHandlerTestBase | ||
{ | ||
[Fact("Does Not Leak")] | ||
public async Task DoesNotLeak() | ||
{ | ||
WeakReference viewReference = null; | ||
|
||
{ | ||
var window = new Window(new ContentPage()); | ||
|
||
await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, _ => | ||
{ | ||
var overlay = new WindowOverlay(window); | ||
viewReference = new(overlay); | ||
window.AddOverlay(overlay); | ||
window.RemoveOverlay(overlay); | ||
}); | ||
} | ||
|
||
await AssertionExtensions.WaitForGC(viewReference); | ||
Assert.False(viewReference.IsAlive, "WindowOverlay should not be alive!"); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters