开发者

Monotouch Garbage Collectiton Not Working - Bug?

开发者 https://www.devze.com 2023-02-16 09:00 出处:网络
I have an application that displays an infinite amount of ViewControllers and animates to each new one, after about 30 VC\'s the application crashes due to lack of memory. After further experimenting,

I have an application that displays an infinite amount of ViewControllers and animates to each new one, after about 30 VC's the application crashes due to lack of memory. After further experimenting, I have discovered that the destructor on the ViewController instance is never called.

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

Code to transition to new 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);
  开发者_Go百科    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();
                });
}

I have tried everything. Manually calling GC.Collect(), Manually calling Dispose() on all subviews, removing subviews, unregistering event handlers...nothing seems to get the VC to release it's memory. Is there a way I can manually release it? Or is there something else I am missing? Extremely frustrated, I welcome any help.


It is hard to tell what your code is doing with that snippet, but if your finalizer is not being called is due to two reasons:

(a) When you call Dispose() manually, this releases the resources, and then calls GC.SupressFinalize (standard Dispose pattern). This prevents finalizers from running, including yours, so you would have never seen it.

(b) If this did not happen even if you take out your Dispose is because someone still has a reference to it.

Now, perhaps what you need is not to animate UIViewControllers, but Views instead. What you seem to be doing behind the UIViewController's back looks nasty, and your code could just be clearer merely have an AnimatedUIViewController that happens to manage multiple views.


Try to check if you're getting called DidReceiveMemoryWarning method on your controllers. If yes you should release some memory on your views that are currently not visible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号