hulop / NavCogIOSv3

NavCog version 3 for iOS
MIT License
12 stars 13 forks source link

HLPLocationManager Crash Problem #58

Closed hiroshi-shin closed 3 years ago

hiroshi-shin commented 3 years ago

Situation

For the first launch, when I open the map from my customized UINavigationController in Swift, it had no problem.

However, the second time when I did the same thing, the app crashed.

Here is the crash log:

Date/Time:           2021-10-15 17:07:05.3898 +0900
Launch Time:         2021-10-15 17:06:57.9714 +0900
OS Version:          iPhone OS 14.6 (18F72)
Release Type:        User
Baseband Version:    1.71.01
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  6

Application Specific Information:
abort() called

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libobjc.A.dylib                 0x00000001a1c231e8 objc_msgSend + 40
1   NavCog3                         0x00000001027dee34 0x1027a0000 + 257588
2   libdispatch.dylib               0x000000018cd6da84 _dispatch_call_block_and_release + 32
3   libdispatch.dylib               0x000000018cd6f81c _dispatch_client_callout + 20
4   libdispatch.dylib               0x000000018cd7dc70 _dispatch_main_queue_callback_4CF + 884
5   CoreFoundation                  0x000000018d0fc398 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
6   CoreFoundation                  0x000000018d0f6270 __CFRunLoopRun + 2524
7   CoreFoundation                  0x000000018d0f5360 CFRunLoopRunSpecific + 600
8   GraphicsServices                0x00000001a4733734 GSEventRunModal + 164
9   UIKitCore                       0x000000018fb70584 -[UIApplication _run] + 1072
10  UIKitCore                       0x000000018fb75df4 UIApplicationMain + 168
11  NavCog3                         0x0000000102843e2c 0x1027a0000 + 671276
12  libdyld.dylib                   0x000000018cdb1cf8 start + 4

Thread 1:
0   libsystem_pthread.dylib         0x00000001d8e05744 start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib         0x00000001d8e05744 start_wqthread + 0

Thread 3 name:  Dispatch queue: com.apple.mobileassetd.v2.maxpcmanager.state
Thread 3:
0   libsystem_kernel.dylib          0x00000001bb38e4fc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001bb38d884 mach_msg + 76
2   libdispatch.dylib               0x000000018cd89280 _dispatch_mach_send_and_wait_for_reply + 520
3   libdispatch.dylib               0x000000018cd8964c dispatch_mach_send_with_result_and_wait_for_reply + 56
4   libxpc.dylib                    0x00000001d8e23e30 xpc_connection_send_message_with_reply_sync + 240
5   MobileAsset                     0x000000019e67e664 __25-[MAXpcManager sendSync:]_block_invoke + 124
6   libdispatch.dylib               0x000000018cd6f81c _dispatch_client_callout + 20
7   libdispatch.dylib               0x000000018cd7e910 _dispatch_lane_barrier_sync_invoke_and_complete + 60
8   MobileAsset                     0x000000019e67e5a4 -[MAXpcManager sendSync:] + 192
9   MobileAsset                     0x000000019e67b990 -[MAAssetQuery queryMetaDataSync] + 708
10  DataDeliveryServices            0x00000001af54dcac -[DDSMobileAssetv2Provider performAssetsForQuery:errorPtr:] + 188
11  DataDeliveryServices            0x00000001af54da9c -[DDSMobileAssetv2Provider assetsForQuery:errorPtr:] + 208
12  DataDeliveryServices            0x00000001af53f02c -[DDSAssetCenter assetsForQuery:error:] + 92
13  LinguisticData                  0x00000001cadb8a28 0x1cadb4000 + 18984
14  LinguisticData                  0x00000001cadb7680 0x1cadb4000 + 13952
15  LinguisticData                  0x00000001cadb9170 LDEnumerateAssetDataItems + 304
16  ProofReader                     0x00000001b54140e4 -[AppleSpell+ 37092 (Lexicon) enumerateAssetDataItemsForLocale:withBlock:] + 104
17  ProofReader                     0x00000001b5415344 -[AppleSpell+ 41796 (LanguageModeling) assetDataBundleURLsForLanguage:] + 184
18  ProofReader                     0x00000001b54188b4 __37-[AppleSpell dataBundlesForLanguage:]_block_invoke.40 + 52
19  libdispatch.dylib               0x000000018cd6da84 _dispatch_call_block_and_release + 32
20  libdispatch.dylib               0x000000018cd6f81c _dispatch_client_callout + 20
21  libdispatch.dylib               0x000000018cd8110c _dispatch_root_queue_drain + 688
22  libdispatch.dylib               0x000000018cd817d8 _dispatch_worker_thread2 + 112
23  libsystem_pthread.dylib         0x00000001d8dfe768 _pthread_wqthread + 216
24  libsystem_pthread.dylib         0x00000001d8e0574c start_wqthread + 8

Thread 4:
0   libsystem_pthread.dylib         0x00000001d8e05744 start_wqthread + 0

Thread 5 name:  com.apple.uikit.eventfetch-thread
Thread 5:
0   libsystem_kernel.dylib          0x00000001bb38e4fc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001bb38d884 mach_msg + 76
2   CoreFoundation                  0x000000018d0fbeb0 __CFRunLoopServiceMachPort + 372
3   CoreFoundation                  0x000000018d0f5d50 __CFRunLoopRun + 1212
4   CoreFoundation                  0x000000018d0f5360 CFRunLoopRunSpecific + 600
5   Foundation                      0x000000018e3e2fdc -[NSRunLoop+ 32732 (NSRunLoop) runMode:beforeDate:] + 232
6   Foundation                      0x000000018e3e2ea8 -[NSRunLoop+ 32424 (NSRunLoop) runUntilDate:] + 92
7   UIKitCore                       0x000000018fc2512c -[UIEventFetcher threadMain] + 516
8   Foundation                      0x000000018e55432c __NSThread__start__ + 864
9   libsystem_pthread.dylib         0x00000001d8dfcbfc _pthread_start + 320
10  libsystem_pthread.dylib         0x00000001d8e05758 thread_start + 8

Thread 6 name:  Dispatch queue: NSOperationQueue 0x107e044c0 (QOS: USER_INTERACTIVE)
Thread 6 Crashed:
0   libsystem_kernel.dylib          0x00000001bb3b3334 __pthread_kill + 8
1   libsystem_pthread.dylib         0x00000001d8dfda9c pthread_kill + 272
2   libsystem_c.dylib               0x0000000196530b90 abort + 104
3   libc++abi.dylib                 0x00000001a1d2abb8 __cxxabiv1::__aligned_malloc_with_fallback+ 80824 (unsigned long) + 0
4   libc++abi.dylib                 0x00000001a1d1bec8 demangling_unexpected_handler+ 20168 () + 0
5   libobjc.A.dylib                 0x00000001a1c2806c _objc_terminate+ 28780 () + 160
6   libc++abi.dylib                 0x00000001a1d29fa0 std::__terminate(void (*)+ 77728 ()) + 20
7   libc++abi.dylib                 0x00000001a1d29f2c std::terminate+ 77612 () + 48
8   libdispatch.dylib               0x000000018cd6f830 _dispatch_client_callout + 40
9   libdispatch.dylib               0x000000018cd72cf4 _dispatch_continuation_pop + 448
10  libdispatch.dylib               0x000000018cd72384 _dispatch_async_redirect_invoke + 592
11  libdispatch.dylib               0x000000018cd80fe0 _dispatch_root_queue_drain + 388
12  libdispatch.dylib               0x000000018cd817d8 _dispatch_worker_thread2 + 112
13  libsystem_pthread.dylib         0x00000001d8dfe768 _pthread_wqthread + 216
14  libsystem_pthread.dylib         0x00000001d8e0574c start_wqthread + 8

Thread 7 name:  AXSpeech
Thread 7:
0   libsystem_kernel.dylib          0x00000001bb38e4fc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001bb38d884 mach_msg + 76
2   CoreFoundation                  0x000000018d0fbeb0 __CFRunLoopServiceMachPort + 372
3   CoreFoundation                  0x000000018d0f5d50 __CFRunLoopRun + 1212
4   CoreFoundation                  0x000000018d0f5360 CFRunLoopRunSpecific + 600
5   Foundation                      0x000000018e3e2fdc -[NSRunLoop+ 32732 (NSRunLoop) runMode:beforeDate:] + 232
6   libAXSpeechManager.dylib        0x00000001b99edbbc -[AXSpeechThread main] + 376
7   Foundation                      0x000000018e55432c __NSThread__start__ + 864
8   libsystem_pthread.dylib         0x00000001d8dfcbfc _pthread_start + 320
9   libsystem_pthread.dylib         0x00000001d8e05758 thread_start + 8

Thread 8 name:  AVAudioSession Notify Thread
Thread 8:
0   libsystem_kernel.dylib          0x00000001bb38e4fc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001bb38d884 mach_msg + 76
2   CoreFoundation                  0x000000018d0fbeb0 __CFRunLoopServiceMachPort + 372
3   CoreFoundation                  0x000000018d0f5d50 __CFRunLoopRun + 1212
4   CoreFoundation                  0x000000018d0f5360 CFRunLoopRunSpecific + 600
5   AudioSession                    0x0000000194b8df20 GenericRunLoopThread::Entry+ 53024 (void*) + 164
6   AudioSession                    0x0000000194b900d8 CAPThread::Entry+ 61656 (CAPThread*) + 92
7   libsystem_pthread.dylib         0x00000001d8dfcbfc _pthread_start + 320
8   libsystem_pthread.dylib         0x00000001d8e05758 thread_start + 8

Thread 9 name:  com.apple.coreaudio.AQClient
Thread 9:
0   libsystem_kernel.dylib          0x00000001bb38e4fc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001bb38d884 mach_msg + 76
2   CoreFoundation                  0x000000018d0fbeb0 __CFRunLoopServiceMachPort + 372
3   CoreFoundation                  0x000000018d0f5d50 __CFRunLoopRun + 1212
4   CoreFoundation                  0x000000018d0f5360 CFRunLoopRunSpecific + 600
5   AudioToolbox                    0x000000019621b930 GenericRunLoopThread::Entry+ 1788208 (void*) + 164
6   libAudioToolboxUtility.dylib    0x00000001a239d104 CADeprecated::CAPThread::Entry+ 65796 (CADeprecated::CAPThread*) + 92
7   libsystem_pthread.dylib         0x00000001d8dfcbfc _pthread_start + 320
8   libsystem_pthread.dylib         0x00000001d8e05758 thread_start + 8

Thread 10 name:  com.apple.CoreMotion.MotionThread
Thread 10:
0   libsystem_kernel.dylib          0x00000001bb38e4fc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001bb38d884 mach_msg + 76
2   IOKit                           0x000000019807f874 io_service_get_matching_service_bin + 200
3   IOKit                           0x000000019800249c IOServiceGetMatchingService + 212
4   CoreMotion                      0x000000019826b930 0x198125000 + 1337648
5   libdispatch.dylib               0x000000018cd6f81c _dispatch_client_callout + 20
6   libdispatch.dylib               0x000000018cd710b4 _dispatch_once_callout + 32
7   CoreMotion                      0x000000019826b7dc 0x198125000 + 1337308
8   CoreMotion                      0x00000001982dd858 0x198125000 + 1804376
9   libdispatch.dylib               0x000000018cd6f81c _dispatch_client_callout + 20
10  libdispatch.dylib               0x000000018cd710b4 _dispatch_once_callout + 32
11  CoreMotion                      0x00000001982dd824 0x198125000 + 1804324
12  CoreMotion                      0x000000019824521c 0x198125000 + 1180188
13  CoreMotion                      0x00000001982725c8 0x198125000 + 1365448
14  CoreFoundation                  0x000000018d0fc718 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28
15  CoreFoundation                  0x000000018d0fb96c __CFRunLoopDoBlocks + 408
16  CoreFoundation                  0x000000018d0f5f54 __CFRunLoopRun + 1728
17  CoreFoundation                  0x000000018d0f5360 CFRunLoopRunSpecific + 600
18  CoreFoundation                  0x000000018d0f644c CFRunLoopRun + 64
19  CoreMotion                      0x0000000198271c78 0x198125000 + 1363064
20  libsystem_pthread.dylib         0x00000001d8dfcbfc _pthread_start + 320
21  libsystem_pthread.dylib         0x00000001d8e05758 thread_start + 8

Thread 11 name:  JavaScriptCore bmalloc scavenger
Thread 11:
0   libsystem_kernel.dylib          0x00000001bb3b30cc __psynch_cvwait + 8
1   libsystem_pthread.dylib         0x00000001d8e00434 _pthread_cond_wait + 1248
2   libc++.1.dylib                  0x00000001a1cc4efc std::__1::condition_variable::wait+ 40700 (std::__1::unique_lock<std::__1::mutex>&) + 28
3   JavaScriptCore                  0x0000000197556c3c void std::__1::condition_variable_any::wait<std::__1::unique_lock<bmalloc::Mutex> >+ 16591932 (std::__1::unique_lock<bmalloc::Mutex>&) + 108
4   JavaScriptCore                  0x000000019755b084 bmalloc::Scavenger::threadRunLoop+ 16609412 () + 208
5   JavaScriptCore                  0x000000019755ac90 bmalloc::Scavenger::Scavenger+ 16608400 (std::__1::scoped_lock<bmalloc::Mutex> const&) + 0
6   JavaScriptCore                  0x000000019755c1a4 std::__1::__thread_specific_ptr<std::__1::__thread_struct>::set_pointer+ 16613796 (std::__1::__thread_struct*) + 0
7   libsystem_pthread.dylib         0x00000001d8dfcbfc _pthread_start + 320
8   libsystem_pthread.dylib         0x00000001d8e05758 thread_start + 8

Thread 6 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x000000016d9a23d0   x5: 0x0000000000000004   x6: 0x0000000000000020   x7: 0x0000000281775b90
    x8: 0x2295dae9a12be2a7   x9: 0x2295dae8ccb1d2a7  x10: 0x00000000000007fd  x11: 0x0000000000000003
   x12: 0x0000000000000001  x13: 0x00000000eca10050  x14: 0x00000000ecc10800  x15: 0x0000000000010800
   x16: 0x0000000000000148  x17: 0x000000016d9a3000  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000000004e0f  x21: 0x000000016d9a30e0  x22: 0x0000000000000134  x23: 0x0000000000000000
   x24: 0x0000000000000000  x25: 0x000000016d9a30e0  x26: 0x0000000000000000  x27: 0x000000016d9a3180
   x28: 0x00000000000006ff   fp: 0x000000016d9a28e0   lr: 0x00000001d8dfda9c
    sp: 0x000000016d9a28c0   pc: 0x00000001bb3b3334 cpsr: 0x40001000
   esr: 0x56000080  Address size fault

I started the HLPLocationManager inside the my customized UIViewController's prepareForSegue function:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Location
        guard let manager = HLPLocationManager.shared() else { return }
        ...
        manager.start()
}

The customized UIViewController is displayed at the end of WelcomeViewController#checkConfig

- (void) checkConfig {
        ... 
        // [self performSegueWithIdentifier:@"show_mode_selection" sender:self];
        ...
        // Here, set the root to Miraikan's Home page
        TabController *vc = [[TabController alloc] init];
        UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
        window.rootViewController = vc;
}

and ended it when BlindViewController is closed:

- (void)viewWillDisappear:(BOOL)animated {
    ...
    [[NSNotificationCenter defaultCenter] postNotificationName:REQUEST_LOCATION_STOP object:self];
}

What I tried

  1. Comment out manager.start()
  2. Put the original Objective-C code inside WelcomeViewController's checkConfig function
- (void) checkConfig {
        ... 
        // [self performSegueWithIdentifier:@"show_mode_selection" sender:self];
        HLPLocationManager *manager = [HLPLocationManager sharedManager];
        ...
        [manager start];
}

For the approach 2, it would not crash for the second launch, but this would cost extra resource when the map is not showing.

daisukes commented 3 years ago

I could not figure out exactly what was happening with the provided information. I need your complete code (or minimum set of codes) to reproduce the issue on my device.

For the first launch, when I open the map from my customized UINavigationController in Swift, it had no problem. However, the second time when I did the same thing, the app crashed.

Is this a hot start problem?

For the approach 2, it would not crash for the second launch, but this would cost extra resource when the map is not showing.

If you call both the start and the stop of HLPLocationManager in a short period of time, it could cause problems.

Thread 6 name:  Dispatch queue: NSOperationQueue 0x107e044c0 (QOS: USER_INTERACTIVE)
Thread 6 Crashed:
0   libsystem_kernel.dylib          0x00000001bb3b3334 __pthread_kill + 8
1   libsystem_pthread.dylib         0x00000001d8dfda9c pthread_kill + 272
2   libsystem_c.dylib               0x0000000196530b90 abort + 104
3   libc++abi.dylib                 0x00000001a1d2abb8 __cxxabiv1::__aligned_malloc_with_fallback+ 80824 (unsigned long) + 0
4   libc++abi.dylib                 0x00000001a1d1bec8 demangling_unexpected_handler+ 20168 () + 0
5   libobjc.A.dylib                 0x00000001a1c2806c _objc_terminate+ 28780 () + 160
6   libc++abi.dylib                 0x00000001a1d29fa0 std::__terminate(void (*)+ 77728 ()) + 20
7   libc++abi.dylib                 0x00000001a1d29f2c std::terminate+ 77612 () + 48
8   libdispatch.dylib               0x000000018cd6f830 _dispatch_client_callout + 40
9   libdispatch.dylib               0x000000018cd72cf4 _dispatch_continuation_pop + 448
10  libdispatch.dylib               0x000000018cd72384 _dispatch_async_redirect_invoke + 592
11  libdispatch.dylib               0x000000018cd80fe0 _dispatch_root_queue_drain + 388
12  libdispatch.dylib               0x000000018cd817d8 _dispatch_worker_thread2 + 112
13  libsystem_pthread.dylib         0x00000001d8dfe768 _pthread_wqthread + 216
14  libsystem_pthread.dylib         0x00000001d8e0574c start_wqthread + 8

Unfortunately, the stack trace does not provide a clear clue to figure out the issue. However, I guess it could happen if you free (stop) resources while starting the manager.

hiroshi-shin commented 3 years ago

In order to explain the situation, I've created a sample view like this:

class TestController: UIViewController {

    private let btn = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        btn.setTitle("Segue", for: .normal)
        btn.setTitleColor(.blue, for: .normal)
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(openMap), for: .touchUpInside)
        self.view.addSubview(btn)

        self.view.backgroundColor = .white
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        btn.center = self.view.center
    }

    @objc private func openMap() {
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "blind_ui")
        self.showDetailViewController(vc, sender: self)
    }

    override func viewWillDisappear(_ animated: Bool) {
        guard let manager = HLPLocationManager.shared() else { return }
        let modalName = UserDefaults.standard.string(forKey: "bleloc_map_data")
        let docPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first

        if let name = modalName,
           let path = docPath {
            manager.setModelPath(path.appendingPathComponent(name).absoluteString)
            let params = InitViewController.getLocationManagerParams()
            manager.parameters = params
            manager.start()
        }
    }

}

In order to stop TTS and any other navigation functions, I added this viewWillDisappear to BlindViewController:

- (void)viewWillDisappear:(BOOL)animated {
    _webView.delegate = nil;

    [navigator stop];
    navigator.delegate = nil;
    navigator = nil;

    commander.delegate = nil;
    commander = nil;

    previewer.delegate = nil;
    previewer = nil;

    dialogHelper.delegate = nil;
    dialogHelper = nil;

    _settingButton = nil;

    [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"developer_mode"];
}

If you call both the start and the stop of HLPLocationManager in a short period of time, it could cause problems. However, I guess it could happen if you free (stop) resources while starting the manager.

I think that I accidentally called stop somewhere.

Moreover, as you suggested, I will consider not to call the start function every time when opening the map, because the app is originally designed to load the map only once.

daisukes commented 3 years ago

I found the issue in setting the model path. The path needs to be like /var/mobile/Containers/Data/..., but with your code, the path is like file:///var/mobile/Containers/Data/...

So, please fix the path by changing the code like the following.

        guard let manager = HLPLocationManager.shared() else { return }
        let modalName = UserDefaults.standard.string(forKey: "bleloc_map_data")
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString

        if let name = modalName {
            manager.setModelPath(path.appendingPathComponent(name))
            let params = InitViewController.getLocationManagerParams()
            manager.parameters = params
            manager.start()
        }