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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:30:28+00:00 2026-06-08T07:30:28+00:00

As I understand it, we need to hold references to our Cocoa objects when

  • 0

As I understand it, we need to hold references to our Cocoa objects when dealing with them in MonoTouch. The reason for this is that the ObjC runtime might still hold references to the objects, and if we have no “MonoTouch references” on them, they could be garbage collected, which results in a EXC_BAD_ACCESS as soon as the ObjC runtime tries to access them.

Say, we have two UIViewController subclasses, VC1 and VC2. If the user clicks on a button on VC1, the UI navigates to VC2, and the user can navigate back and forth. If I create a new instance of VC2 every time the user navigates to it, then the references to the old instances get lost, so they’re garbage collected and the app crashes the next time a didReceiveMemoryWarning is propagated to the UIViewControllers.

How can I release the old references, so I don’t have to use the same instance of VC2 every time? Dispose seemed to not be enough.

  • 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-08T07:30:29+00:00Added an answer on June 8, 2026 at 7:30 am

    As I understand it, we need to hold references to our Cocoa objects when dealing with them in MonoTouch.

    Not quite. MonoTouch managed instances will keep a reference to native instances. As long as the managed instance exists the native instance will be alive (since they are reference counted and MonoTouch won’t release it’s reference).

    IOW your need to hold references to the MonoTouch managed instances as long as their native part are required.

    The reason for this is that the ObjC runtime might still hold references to the objects … they could be garbage collected,

    Native (Objective C) instances are reference counted, not garbage collected. Native instances won’t be released until their reference count reach 0 (which won’t happen while an associated managed instance exists);

    Also native instances can hold references to other native instances. Not every native instances have a corresponding managed instance.

    which results in a EXC_BAD_ACCESS as soon as the ObjC runtime tries to access them.

    That won’t happen, at least not this way. OTOH it’s hard to tell you what’s happening in your case (without seeing the code and/or the crashes).

    I suspect you’re disposing (manually or not) your managed instances before they have completed their jobs. Here’s a simplified of what could happen:

    • you create a managed MT.X instance (e.g. an UIView);
    • this creates and reference a native X (native reference count == 1);
    • you override an event (or add a delegate…) `ViewWillUnload’ on MT.X (which also exists natively);
    • you assign the MT.X instance to another (managed) instance, e.g. an UIViewController;
    • the native UIViewController will add a reference to the native X (native reference count == 2);
    • application executes happilly…
    • you stop having a reference to the MT.X instance (e.g. set the variable to null or a different instance);
    • since there’s no reference to MT.X anymore the Garbage Collector will dispose the managed instance, calling Dispose which will reduce the reference to native X (native reference count == 1). But the native instance won’t be freed since it’s still referenced (not 0) by the view controller;
    • the UIViewController does something that triggers, natively, X.ViewWillUnload (e.g. it tries to load a new UIView);
    • since X still exists natively (ref count == 1) it will call it’s ViewWillUnload which will try to go back to the managed instance… that was disposed.

    The solution to this problem is to ensure ensure you’re not disposing managed instance until their native part have completed their jobs.

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

Sidebar

Related Questions

I don't understand the need for self-joins. Can someone please explain them to me?
I need to understand how this is possible, i.e. I would like to understand
I need to understand the working of this particular program, It seems to be
I understand that we need to create MXML file to define a view. Suppose
I understand that I need to figure out my own homework, but seeing that
I know this question is difficult to understand but I need a mathematical equation
I think that is not easy to understand what I need reading title, so
I understand the need to test a class that has logic (for instance, one
I understand we need to keep @Transactional boundaries as short as possible. Here is
I need to understand how to create a simple animation in HTML 5 or

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.