Bug 13576 - [Device only] SIGSEGV during NSObject.ReleaseManagedRef () when using GADInterstitial
Summary: [Device only] SIGSEGV during NSObject.ReleaseManagedRef () when using GADInte...
Status: RESOLVED NORESPONSE
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.2.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2013-07-29 15:04 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2015-11-12 07:57 UTC (History)
3 users (show)

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


Attachments
Detailed version information (798 bytes, text/plain)
2013-07-29 15:04 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details


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:
Status:
RESOLVED NORESPONSE

Description Brendan Zagaeski (Xamarin Team, assistant) 2013-07-29 15:04:47 UTC
Created attachment 4473 [details]
Detailed version information

In this test project, it appears that a native object is being freed before the managed object (maybe similar to bug #13430 ?). The error does not seem to happen when running on the simulator.


Reported on behalf of a user:

## Steps to reproduce

Please replace “YOUR_AD_UNITID” in AppDelegate.cs and DataLoadingViewController.cs with your pre- or interstitial id.

To reproduce the crash, run the App on a real iPhone:

- Dismiss the prestitial

- Send the App to back several times (up to 5 should be enough) and dismiss the interstitial every time the app shows up

- Swipe in the menu by doing a swipe from left to right on the top blue bar

- Select the second entry of the menu -> now the App should crash

If the crash does not occur try to send the App to the back more often. On our device the App also crashes by just opening and sending to back about 22 to 25 times without using the swipe menu.


## Other possibly useful information

- *Important*: With the recently released Xamarin Update 6.4.0.2, the problem does not occur in the sample project anymore, but it is still present in our real Application. So to reproduce the problem you should test using the older version (detailed version information attached).

- The timer logic is not necessary to produce the crash. The problem also arises if you trigger the GADInterstitial e.g. by a button press. The sample includes the timer to be as close to our real App as possible.

- We tried to catch all unhandled exceptions in Xamarin Studio to get a clue but the SIGSEGV never gets caught by the Debugger.

- We tried to store every GADInterstitial in a static list that never got cleared, and thus the GADInterstitial should never have been garbage collected, but the error persisted. We also tried to do everything related to the GADInterstitial on the UI-Thread but that also didn't help.

- The for-each-loop in InterstitialWatchDog.Uninitialize is for cleaning up used GADInterstitial instances. Commenting the code out does not prevent the crash, but it leads to a memory leak in the application. For debugging, you can leave it out.

- The SIGSEGV appears if the User interacts with the App, or if an automatic refresh of any visible control is triggered.

- The problem appears nearly after every wakeup-interstitial-display if we release (null) the reference to the GADInterstitial in GADInterstitial.WillDismissScreen

- If we do not hold a reference to the GADInterstitial at all, it seems to get garbage collected before any of its events fire.


## Stacktrace

at MonoTouch.Foundation.NSObject.ReleaseManagedRef () [0x00000] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSObject.cs:98
at MonoTouch.Foundation.NSObject/NSObject_Disposer.Drain (MonoTouch.Foundation.NSObject) [0x00062] in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSObject2.cs:545
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at FinanzenNet.Mobile.App.iPhone.Application.Main (string[]) [0x00000] in /Users/finanzennet/dev/mobile/new_trunk2/iPhone/iPhoneDE_v4/App/Main.cs:17
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>

Native stacktrace:

0 FinanzenNetMobileiPhone 0x02424b31 mono_handle_native_sigsegv + 244
1 FinanzenNetMobileiPhone 0x023d5a89 mono_sigsegv_signal_handler + 172
2 libsystem_c.dylib 0x3a92ae93 _sigtramp + 42
3 FinanzenNetMobileiPhone 0x024b3060 is_user_type + 36
4 FinanzenNetMobileiPhone 0x024b2fb8 monotouch_release_managed_ref + 20
5 FinanzenNetMobileiPhone 0x02052d04 wrapper_managed_to_native_MonoTouch_Foundation_NSObject_monotouch_release_managed_ref_intptr + 60
6 FinanzenNetMobileiPhone 0x01ceff04 MonoTouch_Foundation_NSObject_NSObject_Disposer_Drain_MonoTouch_Foundation_NSObject + 364
7 FinanzenNetMobileiPhone 0x01a36660 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
8 FinanzenNetMobileiPhone 0x023d72c7 mono_jit_runtime_invoke + 1054
9 FinanzenNetMobileiPhone 0x0244e6e3 mono_runtime_invoke + 90
10 FinanzenNetMobileiPhone 0x0238b56f native_to_managed_trampoline_MonoTouch_Foundation_NSObject_NSObject_Disposer_Drain + 186
11 Foundation 0x3311e0f5 <redacted> + 460
12 CoreFoundation 0x327dc683 <redacted> + 14
13 CoreFoundation 0x327dbf7f <redacted> + 362
14 CoreFoundation 0x327dacb7 <redacted> + 646
15 CoreFoundation 0x3274debd CFRunLoopRunSpecific + 356
16 CoreFoundation 0x3274dd49 CFRunLoopRunInMode + 104
17 GraphicsServices 0x363112eb GSEventRunModal + 74
18 UIKit 0x34663301 UIApplicationMain + 1120
19 FinanzenNetMobileiPhone 0x020854a8 wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 240
20 FinanzenNetMobileiPhone 0x007e72a8 FinanzenNet_Mobile_App_iPhone_Application_Main_string__ + 160
21 FinanzenNetMobileiPhone 0x01a36660 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
22 FinanzenNetMobileiPhone 0x023d72c7 mono_jit_runtime_invoke + 1054
23 FinanzenNetMobileiPhone 0x0244e6e3 mono_runtime_invoke + 90
24 FinanzenNetMobileiPhone 0x0245144b mono_runtime_exec_main + 306
25 FinanzenNetMobileiPhone 0x02454d6f mono_runtime_run_main + 482
26 FinanzenNetMobileiPhone 0x023f61fb mono_jit_exec + 94
27 FinanzenNetMobileiPhone 0x024a1ac8 main + 2220
28 FinanzenNetMobileiPhone 0x000f57f4 start + 40

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
Comment 2 Eduardo Coelho 2013-12-03 14:09:22 UTC
I'm having a similar issue here.
I'm using Google AdMob 6.5.1.2 (http://components.xamarin.com/view/googleadmob) and my app crashes every time the user touches the GADInterstitial (to visit a website) and them reopen the application.

The stacktrace is as follows:

```
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	0   NamoroIOS                           0x0182a247 mono_handle_native_sigsegv + 254
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	1   NamoroIOS                           0x01834db1 mono_sigsegv_signal_handler + 176
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	2   libsystem_c.dylib                   0x394cbe93 _sigtramp + 42
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	3   NamoroIOS                           0x018df1e0 is_user_type + 76
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	4   NamoroIOS                           0x018df0e0 monotouch_release_managed_ref + 24
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	5   NamoroIOS                           0x00ac4e04 wrapper_managed_to_native_MonoTouch_Foundation_NSObject_monotouch_release_managed_ref_intptr + 92
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	6   NamoroIOS                           0x00a7326c MonoTouch_Foundation_NSObject_ReleaseManagedRef + 32
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	7   NamoroIOS                           0x00a737b0 MonoTouch_Foundation_NSObject_NSObject_Disposer_Drain_MonoTouch_Foundation_NSObject + 364
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	8   NamoroIOS                           0x0075042c wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	9   NamoroIOS                           0x01836e21 mono_jit_runtime_invoke + 1112
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	10  NamoroIOS                           0x0187e7fd mono_runtime_invoke + 88
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	11  NamoroIOS                           0x017ba10b native_to_managed_trampoline_MonoTouch_Foundation_NSObject_NSObject_Disposer_Drain + 262
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	12  Foundation                          0x31d0a0f5 <redacted> + 460
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	13  CoreFoundation                      0x313c8683 <redacted> + 14
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	14  CoreFoundation                      0x313c7ee9 <redacted> + 212
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	15  CoreFoundation                      0x313c6cb7 <redacted> + 646
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	16  CoreFoundation                      0x31339ebd CFRunLoopRunSpecific + 356
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	17  CoreFoundation                      0x31339d49 CFRunLoopRunInMode + 104
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	18  GraphicsServices                    0x34eb62eb GSEventRunModal + 74
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	19  UIKit                               0x3324f301 UIApplicationMain + 1120
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	20  NamoroIOS                           0x00ad010c wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 272
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	21  NamoroIOS                           0x00a837a4 MonoTouch_UIKit_UIApplication_Main_string___string_string + 300
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	22  NamoroIOS                           0x00380734 Aistra_Namoro_IOS_AL_Application_Main_string__ + 344
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	23  NamoroIOS                           0x0075042c wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	24  NamoroIOS                           0x01836e21 mono_jit_runtime_invoke + 1112
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	25  NamoroIOS                           0x0187e7fd mono_runtime_invoke + 88
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	26  NamoroIOS                           0x0188258d mono_runtime_exec_main + 292
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	27  NamoroIOS                           0x018823cd mono_runtime_run_main + 424
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	28  NamoroIOS                           0x018211b9 mono_jit_exec + 48
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	29  NamoroIOS                           0x018c8a84 main + 2480
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 	30  libdyld.dylib                       0x39484b20 <redacted> + 0
Dec  3 16:33:51 Eduardo-Coelhos-iPhone NamoroIOS[3922] <Error>: 
	=================================================================
	Got a SIGSEGV while executing native code. This usually indicates
	a fatal error in the mono runtime or one of the native libraries 
	used by your application.
	=================================================================
```


I tried (without success) to release/nullify the GADInterstitial in the following callbacks:

```

void HandleWillDismissScreen (object sender, EventArgs e)
        {
            Console.WriteLine ("HandleWillDismissScreen");

            //
            // Invoked when:
            // - the user closes the Interstitial by touching the X button.
            // - the user presses the home button and then go reopen the app
            //   (at this point the Interstitial will be automatically dismissed).
            ReleaseCurrentInterstitial ();
        }

        void HandleWillLeaveApplication (object sender, EventArgs e)
        {
            Console.WriteLine ("HandleWillLeaveApplication");
            //
            // Invoked when:
            // - the user closes the Interstitial by touching the X button.
            // - the user presses the home button and then go reopen the app
            //   (at this point the Interstitial will be automatically dismissed).
            ReleaseCurrentInterstitial ();
        }

        void HandleDidFailToReceiveAd (object sender, GADInterstitialDidFailToReceiveAdWithErrorEventArgs e)
        {
            //
            // The current interstitial could not be loaded successfully.
            // Since it is a one-shot instance we must release it and await for a
            // new one will be created the next time 'CreateTargetedInterstitialIfNeeded ()' is called.
            ReleaseCurrentInterstitial ();
        }

void ReleaseCurrentInterstitial ()
        {
            if (currentInterstitial != null) {
                UnregisterForCurrentInterstitialEvents ();
                currentInterstitial.Delegate = null;
                currentInterstitial.Dispose ();
                currentInterstitial = null;
            }
        }

void UnregisterForCurrentInterstitialEvents ()
        {
            if (currentInterstitial != null) {
                currentInterstitial.DidFailToReceiveAd -= HandleDidFailToReceiveAd;
                currentInterstitial.WillDismissScreen -= HandleWillDismissScreen;
                currentInterstitial.WillLeaveApplication -= HandleWillLeaveApplication;
            }
        }
```

This crash happens a lot on my Crashlytics report and I can't find out how to solve this, neither a workaround.
Comment 3 Eduardo Coelho 2013-12-03 14:12:57 UTC
Unfortunately  the crash above happens only on the device.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2013-12-04 09:11:56 UTC
Eduardo: can you attach your project (or alternatively send it to me directly)? I've not been able to reproduce this issue so far.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2015-04-16 10:23:49 UTC
Can you still reproduce this bug? We've been waiting for over a year for a test case.
Comment 6 Rolf Bjarne Kvinge [MSFT] 2015-11-12 07:57:37 UTC
No answer; closing.