2

I have a custom keyboard with images divided in categories. Rigth now I have like 12 images in each category. If I have a category with 20 images, for example the keyboard crashes. I've been looking for a solution for a long time with no luck. When I debug the keyboard in my iPhone I get a SIGQUIT signal:

    dyld`_dyld_start:
->  0x10014d000 <+0>:   mov    x28, sp
    0x10014d004 <+4>:   and    sp, x28, #0xfffffffffffffff0
    0x10014d008 <+8>:   mov    x0, #0x0
    0x10014d00c <+12>:  mov    x1, #0x0
    0x10014d010 <+16>:  stp    x1, x0, [sp, #-0x10]!
    0x10014d014 <+20>:  mov    x29, sp
    0x10014d018 <+24>:  sub    sp, sp, #0x10             ; =0x10 
    0x10014d01c <+28>:  ldr    x0, [x28]
    0x10014d020 <+32>:  ldr    x1, [x28, #0x8]
    0x10014d024 <+36>:  add    x2, x28, #0x10            ; =0x10 
    0x10014d028 <+40>:  adrp   x4, -1
    0x10014d02c <+44>:  add    x4, x4, #0x0              ; =0x0 
    0x10014d030 <+48>:  adrp   x3, 52
    0x10014d034 <+52>:  ldr    x3, [x3, #0xd90]
    0x10014d038 <+56>:  sub    x3, x4, x3
    0x10014d03c <+60>:  mov    x5, sp
    0x10014d040 <+64>:  bl     0x10014d088               ; dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*)
    0x10014d044 <+68>:  mov    x16, x0
    0x10014d048 <+72>:  ldr    x1, [sp]
    0x10014d04c <+76>:  cmp    x1, #0x0                  ; =0x0 
    0x10014d050 <+80>:  b.ne   0x10014d05c               ; <+92>
    0x10014d054 <+84>:  add    sp, x28, #0x8             ; =0x8 
    0x10014d058 <+88>:  br     x16
    0x10014d05c <+92>:  mov    x30, x1
    0x10014d060 <+96>:  ldr    x0, [x28, #0x8]
    0x10014d064 <+100>: add    x1, x28, #0x10            ; =0x10 
    0x10014d068 <+104>: add    x2, x1, x0, lsl #3
    0x10014d06c <+108>: add    x2, x2, #0x8              ; =0x8 
    0x10014d070 <+112>: mov    x3, x2
    0x10014d074 <+116>: ldr    x4, [x3]
    0x10014d078 <+120>: add    x3, x3, #0x8              ; =0x8 
    0x10014d07c <+124>: cmp    x4, #0x0                  ; =0x0 
    0x10014d080 <+128>: b.ne   0x10014d074               ; <+116>
    0x10014d084 <+132>: br     x16

If I click continue I get. Thread 1: shared-library-event:

dyld`_dyld_debugger_notification:
->  0x10022cdc4 <+0>: ret    

Doing 'bt' in the console I get :

* thread #1, stop reason = shared-library-event
  * frame #0: 0x000000010022cdc4 dyld`_dyld_debugger_notification
    frame #1: 0x000000010022c9a4 dyld`gdb_image_notifier(dyld_image_mode, unsigned int, dyld_image_info const*) + 112
    frame #2: 0x000000010022c5f0 dyld`notifyGDB(dyld_image_states, unsigned int, dyld_image_info const*) + 60
    frame #3: 0x0000000100224b4c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1040
    frame #4: 0x000000010023035c dyld`ImageLoader::link(ImageLoader::LinkContext const&, bool, bool, bool, ImageLoader::RPathChain const&, char const*) + 140
    frame #5: 0x0000000100225214 dyld`dyld::link(ImageLoader*, bool, bool, ImageLoader::RPathChain const&, unsigned int) + 228
    frame #6: 0x0000000100226274 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 2544
    frame #7: 0x0000000100221044 dyld`_dyld_start + 68

Any thoughts? UPDATE I created an expection breakpoint and i got the breakpoint where I load the nib file.

- (void)arrangeXib{

    self.keyboard = [[[NSBundle mainBundle]loadNibNamed:@"iPhone" owner:nil options:nil] objectAtIndex:0];

    self.inputView = (UIInputView *)self.keyboard;
}

Exception:

* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGQUIT
    frame #0: 0x00000001847b3224 libsystem_kernel.dylib`mach_msg_trap + 8
    frame #1: 0x00000001847b309c libsystem_kernel.dylib`mach_msg + 72
    frame #2: 0x00000001846a4ee4 libdispatch.dylib`_dispatch_mach_send_and_wait_for_reply + 540
    frame #3: 0x00000001846a52e8 libdispatch.dylib`dispatch_mach_send_with_result_and_wait_for_reply + 56
    frame #4: 0x00000001848ccee0 libxpc.dylib`xpc_connection_send_message_with_reply_sync + 196
    frame #5: 0x0000000185754368 CoreFoundation`__78-[CFPrefsPlistSource sendRequestNewDataMessage:toConnection:retryCount:error:]_block_invoke + 24
    frame #6: 0x00000001857797a8 CoreFoundation`CFPREFERENCES_IS_WAITING_FOR_SYSTEM_CFPREFSD + 48
    frame #7: 0x00000001857542ac CoreFoundation`-[CFPrefsPlistSource sendRequestNewDataMessage:toConnection:retryCount:error:] + 264
    frame #8: 0x000000018575348c CoreFoundation`__50-[CFPrefsPlistSource alreadylocked_requestNewData]_block_invoke + 92
    frame #9: 0x0000000185819d90 CoreFoundation`-[_CFXPreferences withConnectionForRole:performBlock:] + 48
    frame #10: 0x00000001857533e4 CoreFoundation`-[CFPrefsPlistSource alreadylocked_requestNewData] + 288
    frame #11: 0x0000000185750be4 CoreFoundation`_copyValueForKey + 548
    frame #12: 0x00000001857511f0 CoreFoundation`-[CFPrefsPlistSource copyValueForKey:] + 60
    frame #13: 0x0000000185818418 CoreFoundation`__66-[_CFXPreferences copyValueForKey:identifier:user:host:container:]_block_invoke + 40
    frame #14: 0x0000000185813758 CoreFoundation`-[_CFXPreferences(SourceAdditions) withSourceForIdentifier:user:byHost:container:cloud:perform:] + 748
    frame #15: 0x000000018577af20 CoreFoundation`-[_CFXPreferences(SearchListAdditions) with23930198HackSourceForIdentifier:user:byHost:container:cloud:perform:] + 360
    frame #16: 0x0000000185818378 CoreFoundation`-[_CFXPreferences copyValueForKey:identifier:user:host:container:] + 336
    frame #17: 0x000000018754b3d0 ImageIO`IOPreferencesGetBoolean + 196
    frame #18: 0x000000018754c14c ImageIO`InitDebugFlags + 3136
    frame #19: 0x000000018468e9a0 libdispatch.dylib`_dispatch_client_callout + 16
    frame #20: 0x000000018468f6cc libdispatch.dylib`dispatch_once_f + 56
    frame #21: 0x0000000187462ad8 ImageIO`CGImageSourceCreateWithFile + 364
    frame #22: 0x000000018b96c178 UIKit`ImageRefAtPath + 300
    frame #23: 0x000000018b96bff0 UIKit`GetImageAtPath + 36
    frame #24: 0x000000018b96bc60 UIKit`_UIImageAtPath + 396
    frame #25: 0x000000018be10a1c UIKit`-[UIImageNibPlaceholder initWithCoder:] + 524
    frame #26: 0x000000018bf5da50 UIKit`UINibDecoderDecodeObjectForValue + 680
    frame #27: 0x000000018bf5d790 UIKit`-[UINibDecoder decodeObjectForKey:] + 104
    frame #28: 0x000000018bca5ec4 UIKit`-[UIImageView initWithCoder:] + 132
    frame #29: 0x000000018bf5da50 UIKit`UINibDecoderDecodeObjectForValue + 680
    frame #30: 0x000000018bf5dbc8 UIKit`UINibDecoderDecodeObjectForValue + 1056
    frame #31: 0x000000018bf5d790 UIKit`-[UINibDecoder decodeObjectForKey:] + 104
    frame #32: 0x000000018bbed3ec UIKit`-[UIView initWithCoder:] + 652
    frame #33: 0x000000018be0f8d8 UIKit`-[UIClassSwapper initWithCoder:] + 248
    frame #34: 0x000000018bf5da50 UIKit`UINibDecoderDecodeObjectForValue + 680
    frame #35: 0x000000018bf5d790 UIKit`-[UINibDecoder decodeObjectForKey:] + 104
    frame #36: 0x000000018be0f548 UIKit`-[UIRuntimeConnection initWithCoder:] + 136
    frame #37: 0x000000018bf5da50 UIKit`UINibDecoderDecodeObjectForValue + 680
    frame #38: 0x000000018bf5dbc8 UIKit`UINibDecoderDecodeObjectForValue + 1056
    frame #39: 0x000000018bf5d790 UIKit`-[UINibDecoder decodeObjectForKey:] + 104
    frame #40: 0x000000018be0e8bc UIKit`-[UINib instantiateWithOwner:options:] + 1168
    frame #41: 0x000000018be10d40 UIKit`-[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 224
  * frame #42: 0x00000001000b9698 Keyboard`-[KeyboardViewController arrangeXib](self=0x0000000121e058e0, _cmd="arrangeXib") at KeyboardViewController.m:346
    frame #43: 0x00000001000bbbc4 Keyboard`-[KeyboardViewController viewDidLoad](self=0x0000000121e058e0, _cmd="viewDidLoad") at KeyboardViewController.m:514
    frame #44: 0x000000018b907ec0 UIKit`-[UIViewController loadViewIfRequired] + 1036
    frame #45: 0x000000018b907a9c UIKit`-[UIViewController view] + 28
    frame #46: 0x000000018c1de298 UIKit`-[_UIViewServiceViewControllerOperator __createViewController:withContextToken:fbsDisplays:appearanceSerializedRepresentations:legacyAppearance:traitCollection:initialInterfaceOrientation:hostAccessibilityServerPort:canShowTextServices:replyHandler:] + 4172
    frame #47: 0x00000001857dce80 CoreFoundation`__invoking___ + 144
    frame #48: 0x00000001856d22c4 CoreFoundation`-[NSInvocation invoke] + 292
    frame #49: 0x00000001856d6e9c CoreFoundation`-[NSInvocation invokeWithTarget:] + 60
    frame #50: 0x000000018c2e0ce4 UIKit`-[_UIViewServiceImplicitAnimationDecodingProxy forwardInvocation:] + 108
    frame #51: 0x00000001857dad54 CoreFoundation`___forwarding___ + 404
    frame #52: 0x00000001856d6d4c CoreFoundation`_CF_forwarding_prep_0 + 92
    frame #53: 0x00000001857dce80 CoreFoundation`__invoking___ + 144
    frame #54: 0x00000001856d22c4 CoreFoundation`-[NSInvocation invoke] + 292
    frame #55: 0x00000001856d6e9c CoreFoundation`-[NSInvocation invokeWithTarget:] + 60
    frame #56: 0x000000018c232048 UIKit`-[_UIQueueingProxy forwardInvocation:] + 372
    frame #57: 0x00000001857dad54 CoreFoundation`___forwarding___ + 404
    frame #58: 0x00000001856d6d4c CoreFoundation`_CF_forwarding_prep_0 + 92
    frame #59: 0x00000001857dce80 CoreFoundation`__invoking___ + 144
    frame #60: 0x00000001856d22c4 CoreFoundation`-[NSInvocation invoke] + 292
    frame #61: 0x00000001856d6e9c CoreFoundation`-[NSInvocation invokeWithTarget:] + 60
    frame #62: 0x00000001857dad54 CoreFoundation`___forwarding___ + 404
    frame #63: 0x00000001856d6d4c CoreFoundation`_CF_forwarding_prep_0 + 92
    frame #64: 0x00000001857dce80 CoreFoundation`__invoking___ + 144
    frame #65: 0x00000001856d22c4 CoreFoundation`-[NSInvocation invoke] + 292
    frame #66: 0x000000018737f884 FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
    frame #67: 0x000000018737f6f0 FrontBoardServices`-[FBSSerialQueue _performNext] + 176
    frame #68: 0x000000018737faa0 FrontBoardServices`-[FBSSerialQueue _performNextFromRunLoopSource] + 56
    frame #69: 0x000000018578542c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
    frame #70: 0x0000000185784d9c CoreFoundation`__CFRunLoopDoSources0 + 540
    frame #71: 0x00000001857829a8 CoreFoundation`__CFRunLoopRun + 744
    frame #72: 0x00000001856b2da4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #73: 0x000000018711c074 GraphicsServices`GSEventRunModal + 100
    frame #74: 0x000000018b96d058 UIKit`UIApplicationMain + 208
    frame #75: 0x00000001848d7cd0 libxpc.dylib`_xpc_objc_main + 788
    frame #76: 0x00000001848d9984 libxpc.dylib`xpc_main + 200
    frame #77: 0x00000001863dad90 Foundation`-[NSXPCListener resume] + 180
    frame #78: 0x000000018e15712c PlugInKit`-[PKService run] + 752
    frame #79: 0x000000018e156cf0 PlugInKit`+[PKService main] + 56
    frame #80: 0x000000018e157150 PlugInKit`+[PKService _defaultRun:arguments:] + 20
    frame #81: 0x00000001863c0ba4 Foundation`NSExtensionMain + 64
    frame #82: 0x00000001846c159c libdyld.dylib`start + 4

UPDATE 2: I'm pretty sure it has something to do with memory issues... If I display in the cell just a colored background it works ok. Now if I display imgages, if they are less than 10-15 there's no problem but if I have more it will crash with different errors, like the ones above or:

libz.1.dylib`inflate:
libsystem_kernel.dylib`_kernelrpc_mach_vm_deallocate_trap:

At first I had a scroll view and I thought that the problem was that it loaded all the images directly, so I tried to use a collectionView but same results. I tried loading the images 3 different ways with same results:

UIImage imageWithContentsOfFile:
UIImage imageWithData:
UIImage imageNamed:
emiliomarin
  • 362
  • 3
  • 22
  • Try creating an [exception breakpoint](https://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode) and reproduce the crash; you will likely get where your code crashes. – Tamás Sengel Aug 02 '17 at 18:16
  • @the4kman tried and got it where I load the nib file. I will update the question with the code – emiliomarin Aug 02 '17 at 19:21
  • Can you tell us what the actual exception is? – Charles Srstka Aug 02 '17 at 22:56
  • @CharlesSrstka I updated with what I got using the bt command when the exception came, don't know if it's what you meant. If not, please explain how to get it because I didn't get anything else in the console – emiliomarin Aug 02 '17 at 23:58
  • If an exception is thrown, usually a name and short description of the exception get logged to the console explaining what went wrong. If it doesn't for some reason, if you set a breakpoint on objc_exception_throw and type 'po $rdi' in the debugger, you should get it. Alternatively, you could temporarily wrap the line that's crashing in a @try/@catch block and log the exception from there. There's probably a problem with the nib file which the exception, if we can see it, will make clear. – Charles Srstka Aug 03 '17 at 00:04
  • @CharlesSrstka Didn't get anything. with the po command it said: error: use of undeclared identifier '$rdi' And I tried thre try/catch block and didn't write anything in the console... I believe that the problem has something to do with memory leaks, because if I add more images then the crash happens. I am loading images directly with UIImage imageNamed: - and I read that with custom keyboards we have a 30mb limitation, what can I do? – emiliomarin Aug 03 '17 at 09:14
  • @the4kman I updated the post again... don't know if it'll be helpful. I really have no idea what else to try – emiliomarin Aug 03 '17 at 13:18

3 Answers3

3

Got it working now. The problem had to do with memory as I expected. For 3rd party keyboards I read apple limit the memory to 30Mb.

Memory limits for running app extensions are significantly lower than the memory limits imposed on a foreground app. On both platforms, the system may aggressively terminate extensions because users want to return to their main goal in the host app. Some extensions may have lower memory limits than others: For example, widgets must be especially efficient because users are likely to have several widgets open at the same time.

I had png images around 60Kb each but it had a 24bit color bitmap which made each image around 5-6Mb in memory when I loaded them. My solution was to use jpeg images instead, I won't be able to use the transparent background but at least I'll have a working app.

emiliomarin
  • 362
  • 3
  • 22
0

Try to use DispatchQueue to exclude a memory leak problem like this:

    DispatchQueue.main.async {[self] in
        // your code here. Dont forget to use "self" before your variables.

    }

It helps me.

Reason for this assumption came from this WWDC video: https://developer.apple.com/videos/play/wwdc2017/413/ which indicates that with the new dyld, they'll begin crashing apps immediately if load times are too long. Source

Agisight
  • 1,778
  • 1
  • 14
  • 15
0

My app uses PLCrashReporter lib for collecting crash logs from users' devices. If I initialize PLCrashReporter with signal handler type Mach like this:

[[PLCrashReporterConfig alloc] initWithSignalHandlerType:PLCrashReporterSignalHandlerTypeMach];

Then when I try to debug the app, it crashes with a similar stack trace:

Thread 2 Crashed:
0   dyld                            0x00000001018408b8 _dyld_debugger_notification + 0
1   dyld                            0x000000010183fd6c gdb_image_notifier+ 64876 (dyld_image_mode, unsigned int, dyld_image_info const*) + 172
2   dyld                            0x000000010183fa10 notifyGDB+ 64016 (dyld_image_states, unsigned int, dyld_image_info const*) + 64
3   dyld                            0x00000001018359c0 dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)+ 22976 (dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1348
4   dyld                            0x0000000101841e60 ImageLoader::link+ 73312 (ImageLoader::LinkContext const&, bool, bool, bool, ImageLoader::RPathChain const&, char const*) + 176
5   dyld                            0x0000000101835f8c dyld::link+ 24460 (ImageLoader*, bool, bool, ImageLoader::RPathChain const&, unsigned int) + 328
6   dyld                            0x000000010183e1a8 dlopen_internal + 716
7   libdyld.dylib                   0x0000000197b5c3bc dlopen_internal+ 21436 (char const*, int, void*) + 176

When running a debugger, no matter if the app is built in Debug or Release, use handler type PLCrashReporterSignalHandlerTypeBSD.

Vladimir Grigorov
  • 10,903
  • 8
  • 60
  • 70