Ios UIPageViewController泄漏对象
我一直在做一些内存分析,我不明白为什么一些对象没有被垃圾收集——这只是使用UIPageViewController时的一个问题,我认为这可能与此有关,但在该线程中没有最终的答案 在另一个viewcontroller中,我会这样做Ios UIPageViewController泄漏对象,ios,xamarin,Ios,Xamarin,我一直在做一些内存分析,我不明白为什么一些对象没有被垃圾收集——这只是使用UIPageViewController时的一个问题,我认为这可能与此有关,但在该线程中没有最终的答案 在另一个viewcontroller中,我会这样做 var vc = new PageController; NavigationController.pushViewController(vc, true); vc = null; 代码为PageController对象以及Page0和Page1保存WeakRefer
var vc = new PageController;
NavigationController.pushViewController(vc, true);
vc = null;
代码为PageController对象以及Page0和Page1保存WeakReference
然后我打了“回击”。据我所知,应该没有对PageController、Page0或Page1的引用了
我强制垃圾收集器执行其工作:
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
后来我检查了我的weakreference,这三个对象都还活着。我只在UIPageViewController中看到这一点。使用UIViewController或UITableViewController,当我回击并执行完整GC时,所有WeakReferenced对象都会消失
这是VS2013中的Xamarin.IOS,不幸的是我没有访问任何真实内存分析器的权限,所以我看不到是什么让这些对象保持活动状态
我做错了什么
public class PageController : UIPageViewController
{
public PageController()
: base(UIPageViewControllerTransitionStyle.Scroll, UIPageViewControllerNavigationOrientation.Horizontal)
{ }
public override void ViewDidLoad()
{
base.ViewDidLoad();
SetViewControllers(new[] {new Page0()}, UIPageViewControllerNavigationDirection.Forward, true, null);
WeakDataSource = new xSource();
}
public class xSource : UIPageViewControllerDataSource
{
public override UIViewController GetPreviousViewController(UIPageViewController pageViewController,
UIViewController referenceViewController)
{
if (referenceViewController is Page1) return new Page0();
return null;
}
public override UIViewController GetNextViewController(UIPageViewController pageViewController,
UIViewController referenceViewController)
{
if (referenceViewController is Page0) return new Page1();
return null;
}
}
}
public class Page0 : UIViewController
{
public Page0() { }
}
public class Page1 : UIViewController
{
public Page1()
{ }
}
我将回答我自己的问题:问题不直接是UIPageViewController,而是使用了派生类型,这会弄乱垃圾收集。您需要从ViewWillEnglish调用此.navigationController.Dispose()。更多信息: