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

[iOS] App crash when an element is removed from its parent layout #97

Open
Santiago5050 opened this issue Nov 28, 2023 · 4 comments
Open

Comments

@Santiago5050
Copy link

Exception "MauiContext should have been set on parent" when removing a child from its parent layout on iOS

Description:
When a child is removed from its parent layout on iOS, an exception "MauiContext should have been set on parent" is thrown. Upon investigation, it seems that the issue arises during the cleanup process by Maui when attempting to clean a child that has already been cleaned by Mopups.

Proposed Solution:
To address this issue, I suggest stopping listening the 'DescendantRemoved' event and the child handler cleanup in the iOS implementation of IPopupPlatform. By doing so, Maui will be able to properly handle the cleanup of the removed view.

Environment:

  • Platform: iOS
  • .NET Version: 8.0.0
  • Mopups Version: 1.0.2
  • Maui Version: 8.0.3

Repro
ReproMopups.zip

Steps to reproduce

  1. Run the solution in an iOS device.
  2. Tap the 'Open popup' button.
  3. Tap the 'Add/Remove label' button.
    A label will be added with the first tap, but after the second the app will crash trying to remove the label from its parent layout.
@Tamilarasan-Paranthaman
Copy link

Are there any updates regarding this issue?

@microspaze
Copy link

The same issue in RGPopup.Maui:
microspaze/RGPopup.Maui#6

The fix commits:
microspaze/RGPopup.Maui@a64a99d
microspaze/RGPopup.Maui@62ac001

@rbev
Copy link

rbev commented Oct 1, 2024

is this issue getting any attention at all?
i'd prefer not to have to rip mopups out but as mentioned here the Maui team aren't gonna fix this one for you

@rbev
Copy link

rbev commented Oct 2, 2024

For anyone stumbling upon this issue the workaround is to modify the mappers thusly (from here)

//Workaround for: https://github.com/LuckyDucko/Mopups/issues/97
#if IOS
    Mopups.Platforms.iOS.PopupPageHandler.Mapper.AppendToMapping("FixingMopupCrash", (handler, view) =>
    {
        (view as Page).Appearing += (s, e) =>
        {
            var eventName = "DescendantRemoved";
            var eventInfo = typeof(Element).GetEvent(eventName);
            var descendantRemovedEvent = typeof(Element).GetField(eventName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            if (descendantRemovedEvent.GetValue(view) is not Delegate eventDelegate)
            {
                return;
            }

            foreach (var d in eventDelegate.GetInvocationList())
            {
                eventInfo.RemoveEventHandler(view, d);
            }
        };
    });
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants