Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6202345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:43:02+00:00 2026-05-24T04:43:02+00:00

There seems to be something holding a reference to my custom button, MyButton (which

  • 0

There seems to be something holding a reference to my custom button, MyButton (which inherits from UIButton), causing it not to be garbage collected unless I remove it from the superview. This, in turn, would cause the view controller that it is on to also not be finalized and collected.

In my example, I have my custom button but I also have a standard UIButton on the view controller which does not need to be removed from the superview in order to be collected. What’s the difference? Looks pretty similar to me.

See this code. The irrelevant lines were removed for example’s sake. Some things to note about the sample:

–MyButton is pretty empty. Just a constructor and nothing else overridden.

-Imagine MyViewController being on a UINavigationController

–LoadView() just creates the buttons, hooks up an event for each and adds it to the view

-Touching _button would push another MyViewController to the nav controller

-I’m doing some reference cleanup when popping view controllers off the nav controller in ViewDidAppear()

-In CleanUpRefs() you’ll see that I have to remove _myButton from superview in order for all the objects to be garbage collected. _button, on the other hand does not need to be removed.

-I’m expecting the entire MyViewController to be collected, including all subviews, when popping from the nav controller but commenting out _myButton.RemoveFromSuperview() stops this from happening.

public class MyViewController : UIViewController
{
   private UIButton _button;
   private MyButton _myButton;
   private MyViewController _nextController;

   public override void LoadView()
   {
      base.LoadView();

      _button = UIButton.FromType(UIButtonType.RoundedRect);
      _button.TouchUpInside += PushNewController;
      View.AddSubview(_button);

      _myButton = new MyButton();
      _myButton.TouchUpInside += MyButtonTouched;
      View.AddSubview(_myButton);
   }

   private void PushNewController(object sender, EventArgs e)
   {
      _nextController = new MyViewController();
      NavigationController.PushViewController(_nextController, true);
   }

   private void MyButtonTouched(object sender, EventArgs e)
   {
      Console.WriteLine("MyButton touched");
   }

   public void CleanUpRefs()
   {
      //_button.RemoveFromSuperview();
      _myButton.RemoveFromSuperview();
      // remove reference from hooking up event handler
      _button.TouchUpInside -= PushNewController;
      _myButton.TouchUpInside -= MyButtonTouched;
      _button = null;
      _myButton = null;
   }

   public override void ViewDidAppear(bool animated)
   {
      base.ViewDidAppear(animated);

      if(_nextController != null)
      {
         _nextController.CleanUpRefs();
         _nextController = null;
      }
   }
}

It seems as if there’s something different with the fact that MyButton isn’t a straight UIButton in that it is inherited. But then again, why would there be an extra reference count to it that’s being removed by calling RemoveFromSuperview() especially when there’s a UIButton just like it that doesn’t need to be removed?

(I apologize for the really bad layout, stackoverflow seems to have problems laying out bullets right above code snippets)

Update: I filed a bug report with the MonoTouch team. You can download the sample project from there if you want to run it. Bug 92.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:43:03+00:00Added an answer on May 24, 2026 at 4:43 am

    The reason for not garbage collecting in that scenario is just a bug in MonoTouch.

    The upcoming MonoTouch release will contain a fix for this. If you are in a hurry, you can replace your /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll with the copy I placed here:

    http://tirania.org/tmp/monotouch.dll

    I would make a backup, in case I did something wrong in my work-in-progress library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some easy way to pad Strings in Java? Seems like something that
It seems like there should be something shorter than this: private string LoadFromFile(string path)
Is there something like the Python descriptor protocol implemented in other languages? It seems
cgi.escape seems like one possible choice. Does it work well? Is there something that
GDB is starting to give me a headache. There seems to be something weird
There seems to be no good way to localize a WPF application. MSDN seems
There seems to be a lot of heated discussion on the net about the
There seems to be two major conventions for organizing project files and then many
There seems to be three common approaches for mapping an application end user to
There seems to be multiple extremes when supporting embeddable Java HTTP servers. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.