Hi,

On Thu, Apr 19, 2012 at 7:40 AM, René Ruppert
<[email protected]>wrote:

> Hi,
>
> We had this discussion before but even after refactoring all of my code
> I'm still facing exceptions that tell me that there is no IntPtr
> constructor for certain view controllers, but it was tried to be called on
> a garbage collected object.
>
> The outcome of the last discussion was:
> - One should not implement them
> - but you can implement them
> - implementing them will hide the true problem
> - implementing them might lead to crashes in other places
>  - some developers however *always* implement them and never face any
> problems


> Currently I'm seeing the exceptions of the missing constructor totlally
> random for the controllers that are shown as the pages of a
> UiPageViewController, so really nothing fancy.
>
> Xamarin's opinion is (please correct if I'm wrong):
> - I should face these issues mainly (if not even only) in the Simulator
> because of constant GC
> - The cause for the problems is the developer who is not holding
> references.
>

Correct.


> In my case I'm seeing the problems *only* on the device (Arm7, iPad 2 and
> 3, Release or Debug, SGen) and never on the Simulator, even if I trigger
> simulated memory warnings like crazy.
> In the case of the UIPageViewController: what else can I do but return the
> next or previous controller? Am I really supposed to keep an additional
> referenced list of controllers?
>

The fact that it's only happening on devices changes things a (little) bit.
Are these crashes easily reproducible? In that case I can have a look to
see if I find out something.


>
> The UIPageViewController was only an example. There are other controllers
> with similar problems (stupid UITableViewControllers that are not on screen
> at all cause this exception minutes after being removed!). To some I added
> the missing constructor and the issues a gone but I would really like to
> understand more about this problem.
>

What happens when you add the "missing" ctor is that MonoTouch creates a
new instance of the managed class when one is needed (and doesn't already
exist). This means that any state you had in the GC'd instance, is lost.
For instance:

class CustomUITableViewController : UITableViewController {
    string someValue;
    public CustomUITablewViewController (IntPtr ptr) : base (ptr) {}
}

What you will experience is that the someValue field will randomly get
nulled out (when one instance of the class is GC'ed and another one created
in its place). "Randomly" here means between any function calls (such as
between ViewWillAppear and ViewDidAppear).

If you do not have class-level fields in your classes (or you do not care
if those fields are randomly cleared out), it's safe to add the ctor.

Rolf
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to