Monotouch Garbage Collectiton不工作 - Bug?

时间:2011-03-13 19:46:54

标签: c# iphone ipad garbage-collection xamarin.ios

我有一个应用程序向每个新的应用程序显示无限量的ViewControllers和动画,在大约30个VC之后应用程序由于内存不足而崩溃。经过进一步的实验,我发现ViewController实例上的析构函数永远不会被调用。

~TargetPromptController(){
   Console.WriteLine("Destructor Called!");
 }

转换到新VC的代码:

public void PageFlipRight(UIViewController aController) {

      aController.View.Frame = new System.Drawing.RectangleF(0, 0, 659, 630);
      aController.ViewWillAppear(true);
      if (activeRightController != null) activeRightController.ViewWillDisappear(true);
      rightView.AddSubview(aController.View);
      if (aController.View is BaseRightView)
           ((BaseRightView)aController.View).SetLocation(new PointF(0, 0),                           CurrentOrientation);
      aController.ViewDidAppear(true);

      UIView.BeginAnimations(null);
      UIView.SetAnimationDuration(1.0);
      UIView.SetAnimationTransition(UIViewAnimationTransition.CurlUp, rightView, true);
      UIView.CommitAnimations();

      NSTimer.CreateScheduledTimer(new TimeSpan(0, 0, 0, 0, 500),
             delegate {
                  if (activeRightController != null) {
                      activeRightController.View.RemoveFromSuperview();
                      activeRightController.ViewDidDisappear(true);
                      activeRightController.Dispose();
                    }
                  activeRightController = aController;
                  GC.Collect();
                });
}

我已经尝试了一切。手动调用GC.Collect(),在所有子视图上手动调用Dispose(),删除子视图,取消注册事件处理程序......似乎没有任何东西让VC释放它的内存。有没有办法可以手动释放它?或者还有其他我想念的东西?非常沮丧,我欢迎任何帮助。

2 个答案:

答案 0 :(得分:3)

很难说出你的代码在使用该代码片段时所做的事情,但是如果你的终结器没有被调用是由于两个原因:

(a)手动调用Dispose()时,会释放资源,然后调用GC.SupressFinalize(标准Dispose模式)。这可以防止终结器运行,包括你的终结器,所以你从未见过它。

(b)如果即使您取出您的Dispose也不会发生这种情况,因为有人仍然提到它。

现在,也许您需要的不是动画UIViewControllers,而是视图。你在UIViewController后面看起来做的事情看起来很讨厌,你的代码可能只是更清晰,只是有一个AnimatedUIViewController碰巧管理多个视图。

答案 1 :(得分:0)

尝试检查您是否在控制器上调用了DidReceiveMemoryWarning方法。如果是,您应该在您的视图上释放一些当前不可见的内存。