Crash in IMKKit composedString(_:) using swift

132 Views Asked by At

I am writing a swift input method (IM) using InputMethodKit. The IM basically works, I get keystrokes from inputText() and see my log messages in Konsole.

Now I implement composedString(_ sender: Any!) in swift like this:

override func composedString(_ sender: Any!) -> Any! {
  let ret = NSMutableString().append("test")
  return ret
}

I understand that composedString(_ sender: Any!) is called by the system when I call updateComposition(). This is indeed the case but composedString(_ sender: Any!) does crash showing that output:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib              0x00007fff2ccbd680 swift_unknownObjectRetain + 32
1   com.IBNSoft.inputmethod.MyApp   0x000000010c08c491 @objc InputController.composedString(_:) + 33
2   com.apple.InputMethodKit        0x00007fff4670358d -[IMKInputController updateComposition] + 45
3   com.IBNSoft.inputmethod.MyApp   0x000000010c08a5bf closure #1 in InputController.inputText(_:client:) + 703 (InputController.swift:25)
4   com.IBNSoft.inputmethod.MyApp   0x000000010c086740 thunk for @escaping @callee_guaranteed () -> () + 48
5   libdispatch.dylib               0x00007fff20441603 _dispatch_call_block_and_release + 12
6   libdispatch.dylib               0x00007fff204427e6 _dispatch_client_callout + 8
7   libdispatch.dylib               0x00007fff2044eb2f _dispatch_main_queue_callback_4CF + 940
8   com.apple.CoreFoundation        0x00007fff207226f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
9   com.apple.CoreFoundation        0x00007fff206e48e2 __CFRunLoopRun + 2755
10  com.apple.CoreFoundation        0x00007fff206e375c CFRunLoopRunSpecific + 563
11  com.apple.HIToolbox             0x00007fff28905203 RunCurrentEventLoopInMode + 292
12  com.apple.HIToolbox             0x00007fff28904f65 ReceiveNextEventCommon + 587
13  com.apple.HIToolbox             0x00007fff28904d03 _BlockUntilNextEventMatchingListInModeWithFilter + 70
14  com.apple.AppKit                0x00007fff22edfb32 _DPSNextEvent + 864
15  com.apple.AppKit                0x00007fff22ede305 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1364
16  com.apple.AppKit                0x00007fff22ed0679 -[NSApplication run] + 586
17  com.apple.AppKit                0x00007fff22ea485c NSApplicationMain + 816
18  com.IBNSoft.inputmethod.MyApp   0x000000010c08935d main + 13 (AppDelegate.swift:19)
19  libdyld.dylib                   0x00007fff20607f3d start + 1

The Apple docs say about composedString(_:):

Return Value:

The current composed string, which can be an NSString or NSAttributedString object. The returned object should be an autoreleased object.

I guess the problem is that I don't allocate the NSMutableString correctly with respect to "autorelease and retain". Can anybody shed light what the problem with that crash is an how to solve it.

Thanks

0

There are 0 best solutions below