i recieved many crash after i received pasteboard content changed notification

i recieved many crash after i received pasteboard content changed notification. i just monitor the change of pasteboard, and try to recognize the text content.



here is my code:

class PasteboardMonitor {

init() {

addPasteboardMonitor()

}

func addPasteboardMonitor() {

handleContentFromPasteBoard(conent: UIPasteboard.general.string, identifier: "\(UIPasteboard.general.changeCount)")

NotificationCenter.default.addObserver(self, selector: #selector(didReceivedPasteboardNotification(noti:)), name: UIPasteboard.changedNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(didReceivedPasteboardNotification(noti:)), name: UIApplication.willEnterForegroundNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(didReceivedApplicationNotification), name: UIApplication.willResignActiveNotification, object: nil)

}



func handleContentFromPasteBoard(conent: String?, identifier: String? = nil) {

//handle content

...

}

@objc func didReceivedPasteboardNotification(noti: Notification) {

handleContentFromPasteBoard(conent: UIPasteboard.general.string, identifier: "\(UIPasteboard.general.changeCount)")

}


the crash log is:


Crashed: com.apple.UIKit.pasteboard.cache-queue

0 libsystem_pthread.dylib 0x1963b5a38 ___chkstk_darwin + 64

1 libdispatch.dylib 0x19635f49c _dispatch_mach_send_and_wait_for_reply + 440

2 libdispatch.dylib 0x19635f904 dispatch_mach_send_with_result_and_wait_for_reply + 56

3 libxpc.dylib 0x19629f594 xpc_connection_send_message_with_reply_sync + 240

4 Foundation 0x196ba51fc __NSXPCCONNECTION_IS_WAITING_FOR_A_SYNCHRONOUS_REPLY__ + 16

5 Foundation 0x19696b090 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 2704

6 Foundation 0x19696a40c -[NSXPCConnection _sendSelector:withProxy:arg1:] + 128

7 Foundation 0x196bac2ac _NSXPCDistantObjectSimpleMessageSend1 + 40

8 Pasteboard 0x1ac476aac -[PBServerConnection localGeneralPasteboard:]

9 UIKitCore 0x19a6c3428 _getTemporaryLocalGeneralPasteboard + 64

10 UIKitCore 0x19a6c2444 _pasteboardCacheQueue_existingItemCollectionWithName + 152

11 UIKitCore 0x19a6c2160 __59+[_UIConcretePasteboard _pasteboardNamed:createIfNotFound:]_block_invoke + 236

12 libdispatch.dylib 0x196349fd8 _dispatch_client_callout + 20

13 libdispatch.dylib 0x1963567f4 _dispatch_lane_barrier_sync_invoke_and_complete + 60

...

218 UIKitCore 0x19a6c1fcc +[_UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396

219 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti:) + 123 (PasteboardMonitor.swift:123)

220 TovApp 0x1014e4c4c @objc PasteboardMonitor.didReceivedPasteboardNotification(noti:) + 4351249484 (<compiler-generated>:4351249484)

221 Foundation 0x196a6effc __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_2 + 28

222 CoreFoundation 0x1965fc99c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 28

223 CoreFoundation 0x1965fc9ec ___CFXRegistrationPost1_block_invoke + 68

224 CoreFoundation 0x1965fbce4 _CFXRegistrationPost1 + 396

225 CoreFoundation 0x1965fb97c ___CFXNotificationPost_block_invoke + 108

226 CoreFoundation 0x196574910 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1424

227 CoreFoundation 0x1965fb2ac _CFXNotificationPost + 1268

228 Foundation 0x19695db8c -[NSNotificationCenter postNotificationName:object:userInfo:] + 64

229 UIKitCore 0x19a6c1cd4 _sendPasteboardChangedNotification + 96

230 UIKitCore 0x19a6c1fcc +[_UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396

231 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti:) + 123 (PasteboardMonitor.swift:123)

232 TovApp 0x1014e4c4c @objc PasteboardMonitor.didReceivedPasteboardNotification(noti:) + 4351249484 (<compiler-generated>:4351249484)

233 Foundation 0x196a6effc __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_2 + 28

234 CoreFoundation 0x1965fc99c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 28

235 CoreFoundation 0x1965fc9ec ___CFXRegistrationPost1_block_invoke + 68

236 CoreFoundation 0x1965fbce4 _CFXRegistrationPost1 + 396

237 CoreFoundation 0x1965fb97c ___CFXNotificationPost_block_invoke + 108

238 CoreFoundation 0x196574910 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1424

239 CoreFoundation 0x1965fb2ac _CFXNotificationPost + 1268

240 Foundation 0x19695db8c -[NSNotificationCenter postNotificationName:object:userInfo:] + 64

241 UIKitCore 0x19a6c1cd4 _sendPasteboardChangedNotification + 96

242 UIKitCore 0x19a6c1fcc +[_UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396

243 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti:) + 123 (PasteboardMonitor.swift:123)

244 TovApp 0x1014e4c4c @objc PasteboardMonitor.didReceivedPasteboardNotification(noti:) + 4351249484 (<compiler-generated>:4351249484)

245 Foundation 0x196a6effc __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_2 + 28

246 CoreFoundation 0x1965fc99c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 28

247 CoreFoundation 0x1965fc9ec ___CFXRegistrationPost1_block_invoke + 68

248 CoreFoundation 0x1965fbce4 _CFXRegistrationPost1 + 396

249 CoreFoundation 0x1965fb97c ___CFXNotificationPost_block_invoke + 108

250 CoreFoundation 0x196574910 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1424

251 CoreFoundation 0x1965fb2ac _CFXNotificationPost + 1268

252 Foundation 0x19695db8c -[NSNotificationCenter postNotificationName:object:userInfo:] + 64

253 UIKitCore 0x19a6c1cd4 _sendPasteboardChangedNotification + 96

254 UIKitCore 0x19a6c1fcc +[_UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396

255 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti:) + 123 (PasteboardMonitor.swift:123)



do anybody know why this occurs? and how to fix it? thanks.