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

  • SEARCH
  • Home
  • 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 6567155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:18:09+00:00 2026-05-25T14:18:09+00:00

there are two scenarios I keep wondering about when using Monotouch. They deal with

  • 0

there are two scenarios I keep wondering about when using Monotouch. They deal with the requirement to keep a reference to a UIViewController or UIView.

Example one (I found that in the MT bug database). It is implementing a MKMapViewDelegate and the view returned from GetViewForAnnotation will be garbage collected unless a managed reference is kept:

class MapViewDelegate : MKMapViewDelegate
{
    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation)
    {
        var myAnnotation = annotation as MyAnnotation;
        var view = new MKPinAnnotationView (myAnnotation, "myannotation");
        var button = UIButton.FromType (UIButtonType.DetailDisclosure);
        view.RightCalloutAccessoryView = button;

        // I have had this fail as "invalid selector", and also a SIGSEGV crash
        button.TouchDown += delegate {
            Console.WriteLine("I was touched!");
        };

        annotationView = view;

        return annotationView;
    }
}

Example two: if I use PresentModalViewController() I seem to be able to survive without holding a managed reference:

ModalSettingsController oSettingsController = new ModalSettingsController ( );
this.PresentModalViewController(oSettingsController, true);

I have never encountered a NULL reference, SIGSEGV or anything like that in this case.
It looks like in thise case “something” is keeping a reference.

The problem I have is: if I was to use a pure C# environment wihout ObjC world underneath, the code from example one would work without issues; the view has been created and assigned to something else, meaning, a reference exists. But as the view returned in the first example goes back to ObjC and has no managed reference, the GC will clean the managed version away. This puzzles me a bit. How can I figure out in which cases I have to keep a reference and when not?
Do I have to look at Apple’s docs and see if the receviver retains the view or controller?

  • 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-25T14:18:10+00:00Added an answer on May 25, 2026 at 2:18 pm

    Do I have to look at Apple’s docs and see if the receviver retains the view or controller?

    No. In general (> 98% of the API) MonoTouch will deal with the required references to let your managed object live as long as required. This can be done because your calling some managed (C#) code from your application. E.g.

     this.PresentModalViewController(oSettingsController, true);
    

    This allow the PresentModalViewController managed method to keep a reference (if needed) to oSettingsController for as long as needed.

    So what’s the issue with some of the API ? The problem is who’s calling the API. E.g.

    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation)
    

    If this was called form C# then it would not be an issue, i.e. your statement wrt pure C# is mostly correct.

    However it’s called from ObjectiveC since we override a method from a inherited native type. So in this case the managed object view has no (managed) reference (unless you add one, like your example #1) once the method return. But the native caller will retain the native part of the view so things will work… until your application tries to get back into managed code.

    Since the managed peer, e.g. view, has been collected then everything managed inside it will be collected too (e.g. button). That’s why the crash often occurs in events, e.g. in the delegate you assigned to TouchDown.

    That being said we’re working on a solution to cover this case as well in future versions of MonoTouch. Add yourself on c.c. of the bug report if you wish to get notified of the progress.

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

Sidebar

Related Questions

I was wondering about the performance difference between these two scenarios and what could
It seems to me that there are two scenarios in which to use JOINs:
I have two distinct scenarios. One, where there is a many to many case,
I have read a document that they say: In java there two types of
There are two things that seem to be popular nowadays and I was wondering
There are two questions to this: How can I best spread the word about
There are two scenarios I need to clarify: An executable compiled with .NET 3.5
I have two scenarios: There is a Framework project for the company, and for
I was wondering which of these two scenario's works best for swapping between 2
There are two options when setting up your JBPM session. You can put your

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.