Bug 45637 - xamarin_release_managed_ref crashing app
Summary: xamarin_release_managed_ref crashing app
Status: RESOLVED NORESPONSE
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Runtime ()
Version: 2.10.0 (C8)
Hardware: PC Mac OS
: --- critical
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-10-18 15:18 UTC by Frank A. Krueger
Modified: 2017-04-27 16:40 UTC (History)
4 users (show)

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

Description Frank A. Krueger 2016-10-18 15:18:12 UTC
I have an app that crashes at random times (making me think it's GC related) when it's releasing NSObjects.

The crash happens in Foundation.NSObject.xamarin_release_managed_ref but I don't get any more help diagnosing the troublemaker.

Is there anything I can do to get a better log?

I have no simple repro since this seems to only happen when the app is stressed (creating a lot of objects?).



The full log is here: http://pastebin.com/CRMGkDwn


Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) Foundation.NSObject.xamarin_release_managed_ref (intptr,Foundation.NSObject) <IL 0x00008, 0x000c1>
  at Foundation.NSObject.ReleaseManagedRef () [0x00008] in /Users/builder/data/lanes/3821/979cd550/source/xamarin-macios/src/Foundation/NSObject2.cs:208
  at Foundation.NSObject.Dispose (bool) [0x00036] in /Users/builder/data/lanes/3821/979cd550/source/xamarin-macios/src/Foundation/NSObject2.cs:730
  at Foundation.NSObject.Dispose () [0x00003] in /Users/builder/data/lanes/3821/979cd550/source/xamarin-macios/src/Foundation/NSObject2.cs:133
  at ObjCRuntime.Runtime.ThreadPoolDispatcher (System.Func`1<bool>) [0x0001a] in /Users/builder/data/lanes/3821/979cd550/source/xamarin-macios/src/ObjCRuntime/Runtime.cs:237
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x0000c] in /Users/builder/data/lanes/3821/979cd550/source/xamarin-macios/_mac-build/Library/Frameworks/Xamarin.Mac.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1206
  at (wrapper runtime-invoke) <Module>.runtime_invoke_bool (object,intptr,intptr,intptr) <IL 0x0001f, 0x0015d>

(lldb) command source -s 0 '/tmp/mono-gdb-commands.wmmn4p'
Executing commands in '/tmp/mono-gdb-commands.wmmn4p'.
(lldb) process attach --pid 13406
Process 13406 stopped
* thread #1: tid = 0x3f6501, 0x00007fffd50fccba libsystem_kernel.dylib`__psynch_mutexwait + 10, name = 'tid_150b', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x00007fffd50fccba libsystem_kernel.dylib`__psynch_mutexwait + 10
libsystem_kernel.dylib`__psynch_mutexwait:
->  0x7fffd50fccba <+10>: jae    0x7fffd50fccc4            ; <+20>
    0x7fffd50fccbc <+12>: movq   %rax, %rdi
    0x7fffd50fccbf <+15>: jmp    0x7fffd50f5d6f            ; cerror_nocancel
    0x7fffd50fccc4 <+20>: retq   

=== Xamarin Studio Enterprise ===

Version 6.1.1 (build 15)
Installation UUID: 033a3a98-f63f-44a3-8bd3-bb3b731f3639
Runtime:
	Mono 4.6.1 (mono-4.6.0-branch-c8sr0/ef43c15) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406010005

=== NuGet ===

Version: 3.4.3.0

=== Xamarin.Profiler ===

Version: 0.32.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 7.0.0.18 (Xamarin Enterprise)
Android SDK: /Users/fak/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 25.2.2
SDK Platform Tools Version: 24.0.3
SDK Build Tools Version: 24.0.2

Java SDK: /usr
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b23)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b22, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 8.0 (11246)
Build 8A218a

=== Xamarin.Mac ===

Version: 2.10.0.105 (Xamarin Enterprise)

=== Xamarin.iOS ===

Version: 10.0.1.10 (Xamarin Enterprise)
Hash: ad1cd42
Branch: cycle8-sr0-xi
Build date: 2016-10-03 15:18:44-0400

=== Xamarin Inspector ===

Version: 0.8.0.0
Hash: dc081aa
Branch: master
Build date: Tue Apr 26 23:07:44 UTC 2016

=== Build Information ===

Release ID: 601010015
Git revision: fa52f02641726146e2589ed86ec4097fbe101888
Build date: 2016-09-22 08:03:02-04
Xamarin addins: 75d65712af93d54dc39ae4c42b21dfa574859fd6
Build lane: monodevelop-lion-cycle8-sr0

=== Operating System ===

Mac OS X 10.12.0
Darwin muon.local 16.0.0 Darwin Kernel Version 16.0.0
    Mon Aug 29 17:56:20 PDT 2016
    root:xnu-3789.1.32~3/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Xamarin Inspector 0.8.0.0

My versions:

=== Xamarin Studio Enterprise ===

Version 6.1.1 (build 15)
Installation UUID: 033a3a98-f63f-44a3-8bd3-bb3b731f3639
Runtime:
	Mono 4.6.1 (mono-4.6.0-branch-c8sr0/ef43c15) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406010005

=== NuGet ===

Version: 3.4.3.0

=== Xamarin.Profiler ===

Version: 0.32.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 7.0.0.18 (Xamarin Enterprise)
Android SDK: /Users/fak/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 25.2.2
SDK Platform Tools Version: 24.0.3
SDK Build Tools Version: 24.0.2

Java SDK: /usr
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b23)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b22, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 8.0 (11246)
Build 8A218a

=== Xamarin.Mac ===

Version: 2.10.0.105 (Xamarin Enterprise)

=== Xamarin.iOS ===

Version: 10.0.1.10 (Xamarin Enterprise)
Hash: ad1cd42
Branch: cycle8-sr0-xi
Build date: 2016-10-03 15:18:44-0400

=== Xamarin Inspector ===

Version: 0.8.0.0
Hash: dc081aa
Branch: master
Build date: Tue Apr 26 23:07:44 UTC 2016

=== Build Information ===

Release ID: 601010015
Git revision: fa52f02641726146e2589ed86ec4097fbe101888
Build date: 2016-09-22 08:03:02-04
Xamarin addins: 75d65712af93d54dc39ae4c42b21dfa574859fd6
Build lane: monodevelop-lion-cycle8-sr0

=== Operating System ===

Mac OS X 10.12.0
Darwin muon.local 16.0.0 Darwin Kernel Version 16.0.0
    Mon Aug 29 17:56:20 PDT 2016
    root:xnu-3789.1.32~3/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Xamarin Inspector 0.8.0.0
Comment 1 Chris Hamons 2016-10-18 15:24:48 UTC
Hey Frank,

Sorry you are running into issues.

95% of the time you see this:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) Foundation.NSObject.xamarin_release_managed_ref (intptr,Foundation.NSObject) <IL 0x00008, 0x000c1>
  at Foundation.NSObject.ReleaseManagedRef () [0x00008] in /Users/builder/data/lanes/3821/979cd550/source/xamarin-macios/src/Foundation/NSObject2.cs:208

It's because somebody released a reference underneath us (XM) and didn't tell us. Then we get around to disposing the C# "half", which we think we own, and boom. Fireworks.

Figuring out the guilty party is normally straightforward by running your app under Apple's instruments, with the Zombie mode. Often that will determine who the guilty party is, and then we can look to see if there is a binding issue (or a Cocoa bug for example).

If your issue is non-consistently reproducing, this chunk of code often flushes out badness and makes it more reproducible:


new System.Threading.Thread (() => 
{
while (true) {
System.Threading.Thread.Sleep (100);
GC.Collect ();
}
}).Start ();
Comment 2 Frank A. Krueger 2016-10-18 15:41:06 UTC
Great, I will go Zombie hunting. Here is the Apple crash report, just in case.

Crashed Thread:        0  tid_60b  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGABRT)
Exception Codes:       KERN_INVALID_ADDRESS at 0x000007fc12356a98
Exception Note:        EXC_CORPSE_NOTIFY

External Modification Warnings:
Debugger attached to process.

VM Regions Near 0x7fc12356a98:
    VM_ALLOCATE            00000001146e4000-00000001148e4000 [ 2048K] rw-/rwx SM=PRV  
--> 
    STACK GUARD            000070000e581000-000070000e582000 [    4K] ---/rwx SM=NUL  stack guard for thread 1

Application Specific Information:
abort() called

Thread 0 Crashed:: tid_60b  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	0x00007fffd50fcdda __pthread_kill + 10
1   libsystem_pthread.dylib       	0x00007fffd51e7797 pthread_kill + 90
2   libsystem_c.dylib             	0x00007fffd5062440 abort + 129
3   com.kruegersystems.mecha      	0x00000001044b0682 mono_handle_native_sigsegv + 578 (mini-exceptions.c:2420)
4   com.kruegersystems.mecha      	0x000000010441ed9a mono_arch_handle_altstack_exception + 90 (exceptions-amd64.c:808)
5   com.kruegersystems.mecha      	0x00000001044bdaa8 mono_sigsegv_signal_handler + 440 (mini-runtime.c:2883)
6   libsystem_platform.dylib      	0x00007fffd51dabba _sigtramp + 26
7   ???                           	000000000000000000 0 + 0
8   libobjc.A.dylib               	0x00007fffd46eb592 class_getInstanceMethod + 52
9   com.kruegersystems.mecha      	0x00000001043b8154 is_user_type(objc_object*) + 36 (runtime.m:1707)
10  com.kruegersystems.mecha      	0x00000001043b70c9 xamarin_release_managed_ref + 25 (runtime.m:1733)
11  ???                           	0x000000010c7a9396 0 + 4504327062
12  ???                           	0x0000000113e44929 0 + 4628695337
13  com.kruegersystems.mecha      	0x00000001044c09e0 mono_jit_runtime_invoke + 2272 (mini-runtime.c:2547)
14  com.kruegersystems.mecha      	0x0000000104571a68 do_runtime_invoke + 88 (object.c:2949)
15  com.kruegersystems.mecha      	0x0000000104571976 mono_runtime_invoke + 102 (object.c:3060)
16  com.kruegersystems.mecha      	0x00000001043bbc0d xamarin_invoke_trampoline + 5725 (trampolines-invoke.m:450)
17  com.kruegersystems.mecha      	0x00000001043bcc5d xamarin_arch_trampoline + 189 (trampolines-x86_64.m:535)
18  com.kruegersystems.mecha      	0x00000001043be051 xamarin_x86_64_common_trampoline + 110
19  com.apple.Foundation          	0x00007fffc19daf2a __NSThreadPerformPerform + 326
20  com.apple.CoreFoundation      	0x00007fffbffae551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
21  com.apple.CoreFoundation      	0x00007fffbff8f6bd __CFRunLoopDoSources0 + 557
22  com.apple.CoreFoundation      	0x00007fffbff8ebb6 __CFRunLoopRun + 934
23  com.apple.CoreFoundation      	0x00007fffbff8e5b4 CFRunLoopRunSpecific + 420
24  com.apple.HIToolbox           	0x00007fffbf52ff6c RunCurrentEventLoopInMode + 240
25  com.apple.HIToolbox           	0x00007fffbf52fda1 ReceiveNextEventCommon + 432
26  com.apple.HIToolbox           	0x00007fffbf52fbd6 _BlockUntilNextEventMatchingListInModeWithFilter + 71
27  com.apple.AppKit              	0x00007fffbdc265f5 _DPSNextEvent + 1093
28  com.apple.AppKit              	0x00007fffbe3368eb -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1637
29  com.apple.AppKit              	0x00007fffbdc1afbd -[NSApplication run] + 926
30  com.apple.AppKit              	0x00007fffbdbe5a8a NSApplicationMain + 1237
31  ???                           	0x0000000108774d9e 0 + 4437003678
32  ???                           	0x0000000108774c14 0 + 4437003284
33  com.kruegersystems.mecha      	0x00000001044c09e0 mono_jit_runtime_invoke + 2272 (mini-runtime.c:2547)
34  com.kruegersystems.mecha      	0x0000000104571a68 do_runtime_invoke + 88 (object.c:2949)
35  com.kruegersystems.mecha      	0x0000000104573ed4 mono_runtime_exec_main + 788 (object.c:3107)
36  com.kruegersystems.mecha      	0x0000000104573afd mono_runtime_run_main + 909 (object.c:4274)
37  com.kruegersystems.mecha      	0x0000000104416347 mono_jit_exec + 247 (driver.g.c:1048)
38  com.kruegersystems.mecha      	0x00000001044186c2 mono_main + 8098 (driver.g.c:2187)
39  com.kruegersystems.mecha      	0x00000001043be964 xamarin_main + 980 (launcher.m:598)
40  com.kruegersystems.mecha      	0x00000001043bf674 main + 36 (launcher.m:609)
41  libdyld.dylib                 	0x00007fffd4fce255 start + 1
Comment 3 Chris Hamons 2016-10-18 15:50:24 UTC
Yea:

8   libobjc.A.dylib                     0x00007fffd46eb592
class_getInstanceMethod + 52
9   com.kruegersystems.mecha            0x00000001043b8154
is_user_type(objc_object*) + 36 (runtime.m:1707)
10  com.kruegersystems.mecha            0x00000001043b70c9
xamarin_release_managed_ref + 25 (runtime.m:1733)

Suggests you need silver bullets for your zombie hunt.
Comment 4 Frank A. Krueger 2016-10-18 16:00:20 UTC
Found one Zombie: https://www.dropbox.com/s/isiho1bnem9w7yn/Screenshot%202016-10-18%2008.53.03.png?dl=0

NSColorSpaceColor seems to be the culprit coming from the SCNMaterialProperty.

I only ever set SCNMaterialProperty.ContentColor by creating an NSColor so I'm guessing that must be the root problem?

I'm changing my code so I don't set the property as often - we'll see if that helps.
Comment 5 Frank A. Krueger 2016-10-18 16:07:03 UTC
Yes, decreasing the load on setting ContentColor seems to have hidden the bug - I can't get it to crash anymore :-)
Comment 6 Chris Hamons 2016-10-18 16:19:03 UTC
@Frank - Could you paste/point me to an example of your code using ContentColor?

I'm not familiar enough with SceneKit to get a sample working without deep diving into the Apple docs for an hour.
Comment 7 Frank A. Krueger 2016-10-18 16:34:38 UTC
Here's the actual line of code: https://github.com/praeclarum/Ooui/blob/master/Ooui.Shared/SceneNode.cs#L218

Before, I didn't have the `if (diffuseColor != value)` and the property was getting set on mouse move (a lot).

In a simpler version, just create an SCNGeometry using SCNSphere or something. Then access its FirstMaterial.Diffuse.ContentColor property.
Comment 8 Frank A. Krueger 2016-10-18 16:36:46 UTC
This might also be a good time to remind you that ContentColor is bound incorrectly: https://bugzilla.xamarin.com/show_bug.cgi?id=33084
Comment 9 Chris Hamons 2016-10-19 18:44:41 UTC
Hmm. I tried reproducing this with:

https://gist.github.com/chamons/0e2e0e8e04e0725cf802b17cf21643c4

to no avail so far.

I tried building your project "Ooui", but it requires a side by side project:

    <Reference Include="NGraphics">
      <HintPath>..\..\Mecha.Try25\mecha-ui\packages\NGraphics.0.4.0\lib\Xamarin.Mac20\NGraphics.dll</HintPath>
    </Reference>
    <Reference Include="NGraphics.Mac">
      <HintPath>..\..\Mecha.Try25\mecha-ui\packages\NGraphics.0.4.0\lib\Xamarin.Mac20\NGraphics.Mac.dll</HintPath>
    </Reference>

That I can't find.

@Frank - Can you see anything obvious i'm leaving out?
Comment 10 Chris Hamons 2016-10-25 15:26:33 UTC
I'm having trouble reproducing this. Please see Comment 9.
Comment 11 Timothy Risi 2017-04-27 16:40:48 UTC
We have not received the requested information. If you are still 
 experiencing this issue please provide all the requested information 
 and re-open the bug report. Thanks!