Cheesebaron / TZStackView

Port of https://github.com/tomvanzummeren/TZStackView to Xamarin
MIT License
13 stars 0 forks source link

Critical Erro : Foundation.NSObject.xamarin_release_managed_ref #1

Open Aktar opened 8 years ago

Aktar commented 8 years ago

Hey, I'm using TZStackView, I noticed that if I go back and forth from a view containing a StackView, at some point your going to have this error :

2016-04-07 16:16:49.808 MyProject[7208:161052] critical:   at <unknown> <0xffffffff>
2016-04-07 16:16:49.808 MyProject[7208:161052] critical:   at (wrapper managed-to-native) Foundation.NSObject.xamarin_release_managed_ref (intptr,intptr) <0x00012>
2016-04-07 16:16:49.808 MyProject[7208:161052] critical:   at Foundation.NSObject.ReleaseManagedRef () [0x00000] in /Users/builder/data/lanes/2922/f51047c6/source/maccore/src/Foundation/NSObject2.cs:200
2016-04-07 16:16:49.809 MyProject[7208:161052] critical:   at Foundation.NSObject/NSObject_Disposer.Drain (Foundation.NSObject) [0x00062] in /Users/builder/data/lanes/2922/f51047c6/source/maccore/src/Foundation/NSObject2.cs:780

You can prevent this by overriding WillMoveToSuperview in StackView.cs

        public override void WillMoveToSuperview(UIView newsuper)
        {
            if(newsuper == null && this.Subviews != null)
            {
                foreach (var view in this.Subviews)
                {
                    view.RemoveFromSuperview();
                }
            }
            base.WillMoveToSuperview(newsuper);
        }
Cheesebaron commented 8 years ago

Hi @Aktar, thanks for opening this issue. Do you have any more information about this? Why is this necessary in order to avoid that crash? Links to resources would be appreciated.

As far as I can see WillMoveToSuperview is called when a UIView is a subview inside another UIView and that super view changes.

So I guess it is OK to remove all subviews in StackView from the super. But shouldn't they be attached again to the new one? Or how does that work.

marianomino commented 7 years ago

Hi guys, I had the same issue while having a list of items where each one contains a StackView. I managed to workaround the issue by creating a CustomStackView inheriting from StackView and overriding WillMoveToSuperview method like @Aktar suggested.