Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created June 19, 2012 01:17
Show Gist options
  • Save mattetti/2951773 to your computer and use it in GitHub Desktop.
Save mattetti/2951773 to your computer and use it in GitHub Desktop.
crashing RubyMotion with libdispatch and blocks
class Foo
def self.bar(&block)
block.call(42)
end
end
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
Dispatch::Queue.concurrent(:low).async do
Foo.bar{|res| p "the answer is #{res}"}
end
true
end
end
Crashed Thread: 2 Dispatch queue: com.apple.root.low-priority
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000001
VM Regions Near 0x1:
--> __PAGEZERO 0000000000000000-0000000000001000 [ 4K] ---/--- SM=NUL /Users/USER/Library/Application Support/iPhone Simulator/*/sdrubios.app/sdrubios
__TEXT 0000000000001000-00000000001e2000 [ 1924K] r-x/r-x SM=COW /Users/USER/Library/Application Support/iPhone Simulator/*/sdrubios.app/sdrubios
Application Specific Information:
iPhone Simulator 345, iPhone OS 6.0 (iPhone/10A5316k)
Thread 0:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x96eecc22 mach_msg_trap + 10
1 libsystem_kernel.dylib 0x96eec1f6 mach_msg + 70
2 liblaunch.dylib 0x992c2a6d 0x992bb000 + 31341
3 liblaunch.dylib 0x992bfaf3 bootstrap_look_up3 + 83
4 liblaunch.dylib 0x992bfc5a bootstrap_look_up + 80
5 BackBoardServices 0x03cd4aad _BKSServerPortHelper + 145
6 BackBoardServices 0x03cd4a17 BKSHIDServicesServerPort + 48
7 BackBoardServices 0x03ce24db -[BKSAccelerometer _checkIn] + 654
8 BackBoardServices 0x03ce1fc3 -[BKSAccelerometer setAccelerometerEventsEnabled:] + 100
9 SpringBoardServices 0x03cb6934 -[SBSAccelerometer setAccelerometerEventsEnabled:] + 50
10 UIKit 0x01446a8d -[UIMotionEvent _willResume] + 50
11 Foundation 0x00be1f89 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 40
12 CoreFoundation 0x009440c5 ___CFXNotificationPost_block_invoke_0 + 85
13 CoreFoundation 0x0089deba _CFXNotificationPost + 2138
14 Foundation 0x00b16a72 -[NSNotificationCenter postNotificationName:object:userInfo:] + 98
15 UIKit 0x012a2f40 -[UIApplication _stopDeactivatingForReason:] + 346
16 UIKit 0x012a4b5d -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 1236
17 UIKit 0x012b52be -[UIApplication handleEvent:withNewEvent:] + 1131
18 UIKit 0x012b5f9f -[UIApplication sendEvent:] + 85
19 UIKit 0x012a83fd _UIApplicationHandleEvent + 9602
20 GraphicsServices 0x03d13f39 _PurpleEventCallback + 339
21 GraphicsServices 0x03d13c10 PurpleEventCallback + 46
22 CoreFoundation 0x0085eda5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
23 CoreFoundation 0x0085eb12 __CFRunLoopDoSource1 + 146
24 CoreFoundation 0x0088fb46 __CFRunLoopRun + 2118
25 CoreFoundation 0x0088eed4 CFRunLoopRunSpecific + 276
26 CoreFoundation 0x0088edab CFRunLoopRunInMode + 123
27 UIKit 0x012a428f -[UIApplication _run] + 774
28 UIKit 0x012a5e71 UIApplicationMain + 1211
29 sdrubios 0x000034be main + 750
30 sdrubios 0x00003185 start + 53
Thread 1:: Dispatch queue: com.apple.libdispatch-manager
0 libsystem_kernel.dylib 0x96eef926 kevent64 + 10
1 libdispatch.dylib 0x0390da3a _dispatch_mgr_invoke + 858
2 libdispatch.dylib 0x0390d6e0 _dispatch_mgr_thread + 61
Thread 2 Crashed:: Dispatch queue: com.apple.root.low-priority
0 sdrubios 0x0015e8ad rb_vm_add_block_lvar_use + 45
1 sdrubios 0x000d0ee2 rb_proc_alloc_with_block + 82
2 sdrubios 0x00169157 rb_vm_current_block_object + 119
3 sdrubios 0x0005d292 rb_scope__bar__ + 18
4 sdrubios 0x00157b99 rb_vm_dispatch + 3945
5 sdrubios 0x0005d25a vm_dispatch + 506
6 sdrubios 0x0005e2d2 rb_scope__application:didFinishLaunchingWithOptions:__block__ + 274 (app_delegate.rb:32)
7 sdrubios 0x0015b99f rb_vm_block_eval + 1535
8 sdrubios 0x0014bbd1 rb_block_release_eval + 33
9 sdrubios 0x00168ad8 rb_rescue2 + 24
10 sdrubios 0x000c5b50 rb_rescue + 64
11 sdrubios 0x0014bb77 rb_block_dispatcher + 55
12 libdispatch.dylib 0x03919304 _dispatch_client_callout + 14
13 libdispatch.dylib 0x0390b239 _dispatch_root_queue_drain + 286
14 libdispatch.dylib 0x0390b02f _dispatch_worker_thread3 + 20
15 libsystem_c.dylib 0x9c27db24 _pthread_wqthread + 346
16 libsystem_c.dylib 0x9c27f6fe start_wqthread + 30
@mattetti
Copy link
Author

Note that

class Foo
  def self.bar(&block)
    yield(42)
  end
end

Will also make the app crash.

@seanlilmateus
Copy link

a work around should be:

class Foo
  def self.bar(&block)
    block.send(:retain)
    block.call(42)              # yield(42) should work too
  end
end

you should probably open a ticket, there were a similar issue with Symbols

@mattetti
Copy link
Author

Thanks Sean, a ticket was opened and this gist was provided. Laurent should see your message.
The problem seems quite obvious once reduced, a bit less when someone reported a problem with BubbleWrap ;)

@mattetti
Copy link
Author

I haven't heard anything about this bug which really isn't a good sign.

@jimsynz
Copy link

jimsynz commented Jun 24, 2012

Yes, this has gotten me even when running the test suite - I had to tighten up the waits for the reactor specs to avoid it - since I knew it was a problem with RM and not my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment