realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.33k stars 2.15k forks source link

EXC_BAD_ACCESS (SIGSEGV) issue on watch #7246

Closed Turacbey closed 2 years ago

Turacbey commented 3 years ago

On Watch devices, users are facing with crash issues sometimes.

WatchOS version: 7.1 and up (haven't seen any of below OS 7.1) Affected devices: Watch series 3 Used Realm versions: v10.7.4

I'm sharing crash report and function called fetchFromRealm with you.

Crash report;

Date/Time:           2021-05-05 12:35:35.1360 +0430
Launch Time:         2021-05-05 12:35:32.0000 +0430
OS Version:          Watch OS 7.3.3 (18S830)
Release Type:        User
Baseband Version:    n/a

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0xfffffffc
VM Region Info: 0xfffffffc is not in any region.  Bytes after previous region: 2286731261  
      REGION TYPE                START - END     [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      unused shlib __TEXT      772f0000-77b34000 [ 8464K] r--/r-- SM=COW  ... this process
--->  
      UNUSED SPACE AT END

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [545]
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   ...pp - MY APPLICATION  0x00523f98 specialized MYStaticClass.fetchFromRealm<A>(key:value:) + 40 (MYStaticClass.swift:0)
1   ...pp - MY APPLICATION  0x004e4e42 fetchFromRealm + 12 (MYStaticClass.swift:0)
2   ...pp - MY APPLICATION  0x004e4e42 MYStaticClass.init() + 2082 (MYStaticClass.swift:44)
3   ...pp - MY APPLICATION  0x004e435e globalinit_33_A07D3C17314E5551FE6EB93979E767D1_func126 + 28 (MYStaticClass.swift:0)
4   libdispatch.dylib               0x4a53f07c _dispatch_client_callout + 6 (object.m:559)
5   libdispatch.dylib               0x4a540100 _dispatch_once_callout + 14 (once.c:52)
6   libswiftCore.dylib              0x44018d2a swift_once + 26 (once.h:111)
7   ...pp - MY APPLICATION  0x0041d616 HostingController.body.getter + 404 (MYStaticClass.swift:18)
8   ...pp - MY APPLICATION  0x0041d72a vtable thunk for WKHostingController.body.getter dispatching to HostingController.body.getter + 10 (<compiler-generated>:0)
9   SwiftUI                         0x48cccf5a closure #1 in WKHostingController.hostingController.getter + 42 (WKHostingController.swift:32)
10  SwiftUI                         0x48ccbbc0 WKHostingController.hostingController.getter + 32 (WKHostingController.swift:31)
11  SwiftUI                         0x48ccc232 @objc WKHostingController.underlyingUIHostingController() + 18 (<compiler-generated>:0)
12  WatchKit                        0x476cc89a -[SPRemoteInterface underlyingUIHostingControllerForInterfaceController:] + 46 (SPRemoteInterface.m:4974)
13  WatchKit                        0x476cc844 -[SPExtensionConnection underlyingUIHostingControllerForInterfaceController:] + 90 (SPExtensionConnection.m:830)
14  WatchKit                        0x476cc7c4 -[SPApplicationDelegate underlyingUIHostingControllerForHostingViewController:] + 100 (SPApplicationDelegate.m:2757)
15  WatchKit                        0x476ca7da -[SPHostingViewController loadView] + 400 (SPHostingViewController.m:72)
16  UIKitCore                       0x4560288e -[UIViewController loadViewIfRequired] + 160 (UIViewController.m:3464)
17  UIKitCore                       0x455623d6 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 148 (UINavigationController.m:6689)
18  UIKitCore                       0x4556267e -[UINavigationController _startTransition:fromViewController:toViewController:] + 136 (UINavigationController.m:6724)
19  UIKitCore                       0x45563350 -[UINavigationController _startDeferredTransitionIfNeeded:] + 738 (UINavigationController.m:6948)
20  UIKitCore                       0x455644ce -[UINavigationController __viewWillLayoutSubviews] + 126 (UINavigationController.m:7239)
21  UIKitCore                       0x4554b3d4 -[UILayoutContainerView layoutSubviews] + 220 (UILayoutContainerView.m:86)
22  UIKitCore                       0x45ef5166 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2010 (UIView.m:17526)
23  QuartzCore                      0x4648fa42 -[CALayer layoutSublayers] + 232 (CALayer.mm:10147)
24  QuartzCore                      0x46494d24 CA::Layer::layout_if_needed(CA::Transaction*) + 402 (CALayer.mm:10014)
25  QuartzCore                      0x4649f3fe CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 62 (CALayer.mm:2485)
26  QuartzCore                      0x4640a3d8 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 368 (CAContextInternal.mm:2449)
27  QuartzCore                      0x4642bad0 CA::Transaction::commit() + 712 (CATransactionInternal.mm:449)
28  QuartzCore                      0x4642ca72 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56 (CATransactionInternal.mm:932)
29  CoreFoundation                  0x418a4838 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18 (CFRunLoop.c:1799)
30  CoreFoundation                  0x4189fe64 __CFRunLoopDoObservers + 534 (CFRunLoop.c:1912)
31  CoreFoundation                  0x418a0340 __CFRunLoopRun + 832 (CFRunLoop.c:2953)
32  CoreFoundation                  0x4189fb22 CFRunLoopRunSpecific + 474 (CFRunLoop.c:3242)
33  GraphicsServices                0x524cbeb6 GSEventRunModal + 134 (GSEvent.c:2259)
34  UIKitCore                       0x45afc4f2 -[UIApplication _run] + 1044 (UIApplication.m:3253)
35  UIKitCore                       0x45b01042 UIApplicationMain + 114 (UIApplication.m:4707)
36  WatchKit                        0x476c6274 WKExtensionMain + 680 (main.m:107)
37  libdyld.dylib                   0x5331bc32 start + 2

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

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

Thread 3:
0   libsystem_pthread.dylib         0x6940ab48 start_wqthread + 0

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

Thread 5:
0   libsystem_pthread.dylib         0x6940ab48 start_wqthread + 0

Thread 6 name:
Thread 6:
0   libsystem_kernel.dylib          0x471978c0 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x47196f3a mach_msg + 42 (mach_msg.c:103)
2   CoreFoundation                  0x418a4f6c __CFRunLoopServiceMachPort + 338 (CFRunLoop.c:2641)
3   CoreFoundation                  0x418a03e4 __CFRunLoopRun + 996 (CFRunLoop.c:2974)
4   CoreFoundation                  0x4189fb22 CFRunLoopRunSpecific + 474 (CFRunLoop.c:3242)
5   Foundation                      0x41bba596 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 180 (NSRunLoop.m:374)
6   Foundation                      0x41bba4b2 -[NSRunLoop(NSRunLoop) runUntilDate:] + 76 (NSRunLoop.m:421)
7   UIKitCore                       0x45b805b8 -[UIEventFetcher threadMain] + 140 (UIEventFetcher.m:838)
8   Foundation                      0x41cdf628 __NSThread__start__ + 708 (NSThread.m:724)
9   libsystem_pthread.dylib         0x69403e5e _pthread_start + 342 (pthread.c:881)
10  libsystem_pthread.dylib         0x6940ab74 thread_start + 20

Thread 7 name:
Thread 7:
0   libsystem_kernel.dylib          0x471978c0 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x47196f3a mach_msg + 42 (mach_msg.c:103)
2   CoreFoundation                  0x418a4f6c __CFRunLoopServiceMachPort + 338 (CFRunLoop.c:2641)
3   CoreFoundation                  0x418a03e4 __CFRunLoopRun + 996 (CFRunLoop.c:2974)
4   CoreFoundation                  0x4189fb22 CFRunLoopRunSpecific + 474 (CFRunLoop.c:3242)
5   AudioSession                    0x493c6b16 GenericRunLoopThread::Entry(void*) + 126 (GenericRunLoopThread.h:91)
6   AudioSession                    0x493c84bc CAPThread::Entry(CAPThread*) + 62 (CAPThread.cpp:321)
7   libsystem_pthread.dylib         0x69403e5e _pthread_start + 342 (pthread.c:881)
8   libsystem_pthread.dylib         0x6940ab74 thread_start + 20

fetchFromRealm function; (Function executed on main thread)

private func fetchFromRealm<T: Codable>(key:String,value:T) -> T {        

guard let realm = try? Realm() else{
           return value
       }

       var data = Data()

            if key == "abc" {
                let crealm = realm.objects(CRealm.self)
                if crealm.count >= 1{
                    data = crealm[0].data
                    return data
                }
            }
            else if key == "xyz"{
                let chRealm = realm.objects(ChRealm.self)
                var chTmp = [MySwiftModel]()

                for ch in chRealm {
                    chTmp.append(// doing some stuff and adding ch to array)
                }

                return chTmp as! T
    }
    return value
  }

How is the function called? MYStaticClass;


init(){
           fetchFromRealm(key: "abc", value: [array]())
            fetchFromRealm(key: "123", value: [array2]())
            fetchFromRealm(key: "xyz", value: [array3]())
}
TheLuckyWan commented 3 years ago

Based on the error you have, it probably means the main thread tries to access a variable that has been freed or deallocated.

Prehaps you could try to test and run your application in Xcode with the Thread Sanitizer option checked. (Edit Scheme -> Run -> Diagnostics : Thread Sanitizer)

Also, do you know if this happens when there is a memory warning from the watchOS watchdog?

It might help you figure out what's going on.

Turacbey commented 3 years ago

Based on the error you have, it probably means the main thread tries to access a variable that has been freed or deallocated.

Prehaps you could try to test and run your application in Xcode with the Thread Sanitizer option checked. (Edit Scheme -> Run -> Diagnostics : Thread Sanitizer)

Also, do you know if this happens when there is a memory warning from the watchOS watchdog?

It might help you figure out what's going on.

Hello, nothing found with Thread Sanitizer.

This issue only occurs on watch 3 series with real device. I couldn't catch any error with simulator. I don't have a watch 3 series, so I can't do a deep test.

Turacbey commented 3 years ago

@TheLuckyWan

By the way, the function is called like this way;

on MYStaticClass;

init(){
           fetchFromRealm(key: "abc", value: [array]())
            fetchFromRealm(key: "123", value: [array2]())
            fetchFromRealm(key: "xyz", value: [array3]())
}
dianaafanador3 commented 3 years ago

Hi @Turacbey, Thanks for sharing some info about this issue. As mentioned by @TheLuckyWan it looks like the thread is trying to access an object that has been already deallocated. Because the issue happens only in a real device and you don't have access to it, maybe we can't try with the simulator and reproduce this issue. Can you please force a memory warning during test on simulator and see if you get the crash. You can do this on the debug menu -> Simulate Memory Warning

Turacbey commented 3 years ago

Hi @Turacbey, Thanks for sharing some info about this issue. As mentioned by @TheLuckyWan it looks like the thread is trying to access an object that has been already deallocated. Because the issue happens only in a real device and you don't have access to it, maybe we can't try with the simulator and reproduce this issue. Can you please force a memory warning during test on simulator and see if you get the crash. You can do this on the debug menu -> Simulate Memory Warning

Hello @dianaafanador3. Thank you for your answer. I tried running the app simulating memory warning but I couldn't get any crash. If you have any other suggestions, I would love to listen.

dianaafanador3 commented 3 years ago

Hi @Turacbey, Can you share the project with us?, we can try to reproduce this issue in one of our devices, you can send it to diana.perez@mongodb.com.

Turacbey commented 3 years ago

Hi @Turacbey, Can you share the project with us?, we can try to reproduce this issue in one of our devices, you can send it to diana.perez@mongodb.com.

Hello @dianaafanador3, I am sorry, but the project belongs to our company and I can't share everything for now.

The app is live on the App Store and I can send you the link and maybe you can try debugging it on your devices. If the above option doesn't work then I can create a new project for you with the crash part.

dianaafanador3 commented 3 years ago

Hi @Turacbey, that will be great, we just want a way to be able to reproduce the issue

Turacbey commented 3 years ago

Hello @dianaafanador3,

I sent you the project.

dianaafanador3 commented 3 years ago

Thanks @Turacbey we'll take a look at it.

Turacbey commented 3 years ago

Thanks @dianaafanador3

leemaguire commented 3 years ago

@Turacbey I'm trying to debug this on a Watch Series 2 with not much luck. Would you be able to tell us how big the Realm file is that causes the crash? I'm wondering if this is related to the Series 3 being 32bit and having limited address space.

Turacbey commented 3 years ago

@leemaguire The crash happens only on watch series 3. There is no any crash from series se, series 5 or series 6 etc.

I don't know how big is the realm file, but it can not too big. I added some buttons that add items to arrays, I guess it is not bigger than 1000 items on each array.

Can you able to test it on watch series 3?

leemaguire commented 3 years ago

@Turacbey unfortunately I do not have a Watch Series 3 to test with.

Turacbey commented 3 years ago

We will be waiting for someone who has watch series 3.

leemaguire commented 2 years ago

@Turacbey have you been able to solve this with the latest version of Realm? We require more information to proceed with an investigation as we do not possess a Watch Series 3.

Turacbey commented 2 years ago

Hi @leemaguire! I switched to CoreData over 7 months ago and changed my data structure long after that (not error related). So for now there is no possibility to try it again with the latest version.

leemaguire commented 2 years ago

Ok- given that information I'll go ahead and close the issue.