Bug 14399 - Class.ThrowOnInitFailure does not always work
Summary: Class.ThrowOnInitFailure does not always work
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.4.4
Hardware: PC Mac OS
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Rolf Bjarne Kvinge [MSFT]
Depends on:
Reported: 2013-09-03 10:56 UTC by Sebastien Pouliot
Modified: 2014-02-10 12:15 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Sebastien Pouliot 2013-09-03 10:56:03 UTC
`Class.ThrowOnInitFailure` does not throw the expected exception in cases where `NSObject(NSObjectFlag)` is used.

ref: bug #13707
Comment 1 Sebastien Pouliot 2013-11-08 14:30:02 UTC
It's bug #14307 (not 13707) and it's also the cause of confusion in bug #15973.

The issue is that `Class.ThrowOnInitFailure` actually checks after `alloc` not after `init*`. So if `alloc` succeed (the Handle is non-nil) and `init*` fails (Handle becomes nil) then the condition remains unreported.

Can be duplicated with UrlTest.cs, InitWith
Comment 2 Rolf Bjarne Kvinge [MSFT] 2013-11-11 12:02:38 UTC
Unfortunately we don't have any good place to detect 'init*' failures as opposed to 'alloc' failures. The only thing we could do would be to detect setting the Handle property to IntPtr.Zero (which is technically yet another breaking change).

Let me try it and see if it even works with our own code...
Comment 3 Sebastien Pouliot 2013-11-11 13:01:55 UTC
Yup, I did not see any "good place" either when I filed this :-( and modifying the Handle property looks pretty risky IMO.

Maybe we could have the generated .ctors call a new InitializeHandle method (instead of setting the Handle directly). 

This method would check if:

1. alloc succeeded (replacing the previous check) by ensuring `this.Handle` is not 0x0;

2. init* succeeded, by chekcing the `handle` parameter (not 0x0) before setting the Handle property.

That should be safe since existing bindings (and non generated code) would continue to use the same, unmodified, code.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2013-11-15 06:54:51 UTC
FIxed (like you suggested, with an InitializeHandle method).

maccore/master: 42efb9ee86d22304ba3e56c6f804b0041c25aad7
monotouch/master: 99d6262348d5224a9415f3fe6aee529d511f5b46
Comment 5 PJ 2013-12-11 18:46:01 UTC
This fix is planned to be released with Xamarin.iOS 7.0.6, which should hit the beta channel before December 23rd.
Comment 6 Rolf Bjarne Kvinge [MSFT] 2014-01-07 17:36:18 UTC
This is technically a breaking change (see bug #17089), so maybe we should live this fix for later (XI 8.0 for instance)?
Comment 7 Rolf Bjarne Kvinge [MSFT] 2014-02-10 12:15:15 UTC
This was shipped with 7.0.6, so we'll just leave it in now.