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 8754253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:34:55+00:00 2026-06-13T13:34:55+00:00

I think I’m getting close to understanding how Mono GC and ObjC ref counting

  • 0

I think I’m getting close to understanding how Mono GC and ObjC ref counting live together.

The way it works is that when a native object has a reference count of 1, we do not prevent the managed instance from getting garbage collected. As soon as the reference count increases above 1, we prevent the managed instance from getting garbage collected.

This is because a managed object may contain user state. For managed objects which are mirroring a corresponding native object (such as the managed UIView instance) MonoTouch knows that the instance can not contain any state, so as soon as no managed code has a reference to the managed instance, the GC can collect it. If a managed instance is required at a later stage, we just create a new one.

So if I create a CustomButton that inherits UIButton, add it as subview to my View, let the managed reference slip out of scope and then run GC, this managed CustomButton still won’t be eligible for collection.

Why can’t it be collected? Of course it may have managed state like properties, but if there is no link to it from managed objects, who cares about this state? It may as well just disappear, why can’t it?

I’m thinking of one possible reason: subscribing to CustomButton events won’t keep it alive for the GC so when the object gets collected, events stop firing. This would perhaps result in unexpected behavior.

Is this correct? Are there other reasons for keeping the managed object alive even if no one links it?

  • 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-06-13T13:34:56+00:00Added an answer on June 13, 2026 at 1:34 pm

    Why can’t it be collected? Of course it may have managed state like properties, but if there is no link to it from managed objects, who cares about this state? It may as well just disappear, why can’t it?

    Native code might have references to the object, which may cause the object to resurface to managed code again later.

    I believe a code sample would illustrate what would happen:

    class MyView : UIView {
        public string ImportantSecret;
    }
    
    class AppDelegate : UIApplicationDelegate {
        UIViewController vc;
        public override bool FinishedLaunching (UIApplication app, 
                                                NSDictionary options)
        {
            var myView = new MyView ();
            myView.ImportantSecret = "MonoTouchRocks";
    
            vc = new UIViewController ();
            vc.View = new UIView ();
            vc.View.AddSubView (myView);
    
            // When this method returns the only place where myView is referenced
            // is from inside the *native* Subviews collection.
    
            BeginInvokeOnMainThread (() =>
            {
                Console.WriteLine (((MyView) vc.Subviews [0]).ImportantSecret);
                // If the MyView instance was garbage collected and recreated
                // automatically at this point, ImportantSecret would be null.
            });
        }
    }
    

    Important: this code is just to illustrate the reason why the GC can’t collect managed objects which may have state. This particular sample would actually not forget the important secret, since the Subviews array is automatically cached in managed code – but this is not generally true.

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

Sidebar

Related Questions

I think I have a basic understanding of this, but am hoping that someone
I think that handlers in android are tools to get different objects that are
Think that I have many activities,and all I want is this: I have a
Think that says it all?
Think of a Pinterest, when someone takes a picture and uploads that picture, where
I think that asking this might be kind of silly but I'm still wondering
I think I have found a bug when setting or getting the Me.Top property
Think that you have a content div whether the content of a website is
I think this is more of my lack of understanding the intricacy of Types
Think Global, Act Local That's what they tell you, however during all my time

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.