Bug 12371 - SIGSEGV Crash With Threading
Summary: SIGSEGV Crash With Threading
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Debugger ()
Version: 6.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: 7.2.0 (iOS 7.1)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-05-23 19:20 UTC by deekor88
Modified: 2017-03-31 23:46 UTC (History)
6 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 ANSWERED

Description deekor88 2013-05-23 19:20:58 UTC
Since Incorporating threading into my app to download images. I have been receiving this crash randomly:

Native stacktrace:

	0   hcw2                                0x00095d9c mono_handle_native_sigsegv + 284
	1   hcw2                                0x00009678 mono_sigsegv_signal_handler + 248
	2   libsystem_c.dylib                   0x947708cb _sigtramp + 43
	3   ???                                 0xffffffff 0x0 + 4294967295
	4   ???                                 0x0feaf400 0x0 + 267056128
	5   ???                                 0x0b7a9de8 0x0 + 192585192
	6   hcw2                                0x0000da42 mono_jit_runtime_invoke + 722
	7   hcw2                                0x00170f1e mono_runtime_invoke + 126
	8   hcw2                                0x00213c66 monotouch_trampoline + 3686
	9   Foundation                          0x00ac00d5 -[NSThread main] + 76
	10  Foundation                          0x00ac0034 __NSThread__main__ + 1304
	11  libsystem_c.dylib                   0x947845b7 _pthread_start + 344
	12  libsystem_c.dylib                   0x9476ed4e thread_start + 34

=================================================================
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.
=================================================================

This is only in the simulator, code runs fine on my iPhone 5 and I have never had it crash.
Comment 1 Sebastien Pouliot 2013-05-23 21:33:21 UTC
    3   ???                                 0xffffffff 0x0 + 4294967295

^ that often occurs when calling back to an instance that was garbage collected. 

If you do not keep a reference to an object instance then the GC is free to collect it whenever it wants (which is perfectly normal since it is it's job to do so).

It's not possible to be 100% sure without seeing the source code - but look if you're not using a local variable and trying (background thread) to use it after the method returned. Such cases are common and the fix is often to promote the local variable to a field (so it stays alive as long as needed).

> This is only in the simulator, code runs fine on my iPhone 5 and I have never
> had it crash.

The simulator runs the GC much more frequently to help you find such issues. IOW it will happen on devices too, just less frequently and much harder to debug / reproduce.
Comment 2 deekor88 2013-05-24 16:46:59 UTC
Going through my code I cant find any local variables that would be causing it. All the variables being used are fields.
Comment 3 Sebastien Pouliot 2013-05-24 16:48:51 UTC
It could be somethign else. Please attach (you can mark it private) a self-contained test case and I'll look at it asap.
Comment 6 Sebastien Pouliot 2013-05-24 17:28:24 UTC
Not using anonymous delegates reduce the likelyness of using a local variable.

Nothing really stands out from the fragment, OTOH it is incomplete (e.g. UpdateNotification is missing).

By self contained attachment I meant a solution that we can build and see the issue. Ideally based on your sources where you remove/comment everything that can be removed/commented and still reproduce the issue. 

If you can attach such a test case then we can look at it.
Comment 7 deekor88 2013-05-28 14:46:38 UTC
Trying my code again I got the error:

The requested operation cannot be completed because the object has been garbage collected.

Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot be completed because the object has been garbage collected.
  at Mono.Debugger.Soft.VirtualMachine.ErrorHandler (System.Object sender, Mono.Debugger.Soft.ErrorHandlerEventArgs args) [0x00041] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachine.cs:289 
  at Mono.Debugger.Soft.Connection.SendReceive (CommandSet command_set, Int32 command, Mono.Debugger.Soft.PacketWriter packet) [0x000d5] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs:1447 
  at Mono.Debugger.Soft.Connection.Object_GetInfo (Int64 id) [0x00008] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs:2315 
  at Mono.Debugger.Soft.VirtualMachine.GetObject[ObjectMirror] (Int64 id, Int64 domain_id, Int64 type_id) [0x00061] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachine.cs:481 
  at Mono.Debugger.Soft.VirtualMachine.GetObject[ObjectMirror] (Int64 id) [0x00000] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachine.cs:509 
  at Mono.Debugger.Soft.VirtualMachine.GetObject (Int64 objid) [0x00000] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachine.cs:513 
  at Mono.Debugger.Soft.ExceptionEvent.get_Exception () [0x0000b] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/contrib/Mono.Debugger.Soft/Mono.Debugger.Soft/ExceptionEvent.cs:15 
  at Mono.Debugging.Soft.SoftDebuggerSession.HandleBreakEventSet (Mono.Debugger.Soft.Event[] es, Boolean dequeuing) [0x0009a] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/src/addins/MonoDevelop.Debugger.Soft/Mono.Debugging.Soft/SoftDebuggerSession.cs:1348 
  at Mono.Debugging.Soft.SoftDebuggerSession.HandleEventSet (Mono.Debugger.Soft.EventSet es) [0x00071] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/src/addins/MonoDevelop.Debugger.Soft/Mono.Debugging.Soft/SoftDebuggerSession.cs:1296 
  at Mono.Debugging.Soft.SoftDebuggerSession.EventHandler () [0x0002d] in /Users/builder/data/lanes/monodevelop-lion-monodevelop-4.0.8-branch/0a09117d/source/monodevelop/main/src/addins/MonoDevelop.Debugger.Soft/Mono.Debugging.Soft/SoftDebuggerSession.cs:1226 



Does this reveal any more info on what object might have caused it?
Comment 8 Sebastien Pouliot 2013-05-30 20:28:21 UTC
Not much. This exception comes from Xamarin Studio debugger (not your application). But it does complain about an object that was collected.

> Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot be
> completed because the object has been garbage collected.
Comment 10 PJ 2013-11-19 16:44:58 UTC
This bug was targeted for a past milestone, moving to the next non-hotfix active milestone.
Comment 11 GouriKumari 2016-01-11 21:35:04 UTC
If this issue occurs with the latest XI and XS stable builds, please reopen the issue. BAsed on comment #8, closing this message.
Comment 12 Troy O'Neal 2017-03-31 21:16:31 UTC
I am getting the exact same error stack trace as the original poster:

Native stacktrace:

2017-03-31 14:02:15.785 GLV_ios[64636:1184523] critical: 	0   GLV_ios                             0x000000010e5825ed mono_handle_native_sigsegv + 253
2017-03-31 14:02:15.785 GLV_ios[64636:1184523] critical: 	1   GLV_ios                             0x000000010e58e266 mono_sigsegv_signal_handler + 294
2017-03-31 14:02:15.786 GLV_ios[64636:1184523] critical: 	2   libsystem_platform.dylib            0x000000011b995b3a _sigtramp + 26
2017-03-31 14:02:15.786 GLV_ios[64636:1184523] critical: 	3   GLV_ios                             0x000000010e57f470 mono_walk_stack_with_state + 0
2017-03-31 14:02:15.786 GLV_ios[64636:1184523] critical: 	4   GLV_ios                             0x000000010e63b891 mono_runtime_delegate_try_invoke + 49
2017-03-31 14:02:15.787 GLV_ios[64636:1184523] critical: 	5   GLV_ios                             0x000000010e68137a start_wrapper + 538
2017-03-31 14:02:15.787 GLV_ios[64636:1184523] critical: 	6   GLV_ios                             0x000000010e6ea282 inner_start_thread + 354
2017-03-31 14:02:15.787 GLV_ios[64636:1184523] critical: 	7   libsystem_pthread.dylib             0x000000011b9a79af _pthread_body + 180
2017-03-31 14:02:15.788 GLV_ios[64636:1184523] critical: 	8   libsystem_pthread.dylib             0x000000011b9a78fb _pthread_body + 0
2017-03-31 14:02:15.788 GLV_ios[64636:1184523] critical: 	9   libsystem_pthread.dylib             0x000000011b9a7101 thread_start + 13
2017-03-31 14:02:15.788 GLV_ios[64636:1184523] critical: 
=================================================================
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'm using the latest version of Xamarin ios and running on iPhone 7 Simulator 10.3.  I was confused and tried to figure out what was going on, but couldn't.  I'm able to reproduce the error with the following simple test case:

        public void reproduce()
        {
            int i = 0;
            while (true)

            {
                Thread t = new Thread(threadLoop);
                t.Start();
                t.Join();
                t = null;
                Console.WriteLine("Looping " + (i++));
            }
        }

        public void threadLoop()
        {

        }


Am I doing something obviously wrong here?  It seems like the above code shouldn't crash with a SIGSEGV.  The thread is never accessed after being garbage collected...
Comment 13 Sebastien Pouliot 2017-03-31 21:21:52 UTC
@Troy this does not look identical to the original one. Please create a new bug report and attach a test case along with all version information. Thanks!
Comment 14 Troy O'Neal 2017-03-31 23:46:38 UTC
Created https://bugzilla.xamarin.com/show_bug.cgi?id=54359