Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashing with EXC_BAD_ACCESS KERN_INVALID_ADDRESS while initializing Realm #6308

Closed
DracoRubi opened this issue Oct 17, 2019 · 6 comments
Closed

Comments

@DracoRubi
Copy link

DracoRubi commented Oct 17, 2019

Goals

I want to solve a crash that happens ocasionally.

Expected Results

A normal behaviour, without crash nor error.

Actual Results

Crashed: 87069A2F-ABC7-49A8-92B2-2F04CA003864 (QOS: DEFAULT)
0  libobjc.A.dylib                0x18263ecd8 objc_destructInstance + 28
1  libswiftCore.dylib             0x18fcc7db8 swift_deallocClassInstance + 32
2  libswiftCore.dylib             0x18fcc7ce8 _swift_release_dealloc + 28
3  TARGETAPP                      0x1049065bc RealmManager.__deallocating_deinit + 12 (Realm.swift:12)
4  libswiftCore.dylib             0x18fcc7ce8 _swift_release_dealloc + 28
5  TARGETAPP                      0x1048ab2a0 outlined assign with take of RealmInterface? (<compiler-generated>)
6  TARGETAPP                      0x1048a83f4 DataSource.getTransactionHistory(enrollmentID:domainQueryHistory:controllerSignatures:) + 297 (DataSource.swift:297)
7  TARGETAPP                      0x10487a450 closure #1 in HistoryTransactionInteractor.getTransactionHistory(enrollmentId:domainQuery:controllerSignatures:onSuccess:onError:onOauthError:) (<compiler-generated>)
8  TARGETAPP                      0x10487a754 partial apply for closure #1 in HistoryTransactionInteractor.getTransactionHistory(enrollmentId:domainQuery:controllerSignatures:onSuccess:onError:onOauthError:) (<compiler-generated>)
9  TARGETAPP                      0x10487a798 partial apply for thunk for @escaping @callee_guaranteed () -> (@owned DomainHistory, @error @owned Error) (<compiler-generated>)
10 Kommander                      0x104d09838 closure #1 in Kommand.execute() + 103 (Kommand.swift:103)
11 Kommander                      0x104d08974 thunk for @escaping @callee_guaranteed () -> () (<compiler-generated>)
12 Foundation                     0x182cc961c __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
13 Foundation                     0x182bd33d8 -[NSBlockOperation main] + 100
14 Foundation                     0x182ccb8a4 __NSOPERATION_IS_INVOKING_MAIN__ + 20
15 Foundation                     0x182bd3070 -[NSOperation start] + 732
16 Foundation                     0x182ccc29c __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 20
17 Foundation                     0x182ccbd68 __NSOQSchedule_f + 180
18 libdispatch.dylib              0x1825d1610 _dispatch_call_block_and_release + 24
19 libdispatch.dylib              0x1825d2184 _dispatch_client_callout + 16
20 libdispatch.dylib              0x18257aa9c _dispatch_continuation_pop$VARIANT$mp + 412
21 libdispatch.dylib              0x18257a1f0 _dispatch_async_redirect_invoke + 600
22 libdispatch.dylib              0x182586fd0 _dispatch_root_queue_drain + 376
23 libdispatch.dylib              0x18258779c _dispatch_worker_thread2 + 128
24 libsystem_pthread.dylib        0x182621f64 _pthread_wqthread + 212
25 libsystem_pthread.dylib        0x182624ae0 start_wqthread + 8

Steps to Reproduce

Couldn't reproduce. It happens randomly and very exceptionally.

Code Sample

class DataSource {
    var database: RealmInterface!

    private func initRealm() throws -> RealmManager {
        let realmKey = try keyForRealm()
        return RealmManager(realmKey: realmKey)
    }

    func someExampleFunction() {
        realm = initRealm() // It would crash here
        realm.addFetchedData()
    }
}
class RealmManager: RealmInterface {

    private var database: Realm!

    init(realmKey: Data) {
        let realmConfig = Realm.Configuration(inMemoryIdentifier: "MyInMemoryRealm", encryptionKey: realmKey)
        database = try! Realm(configuration: realmConfig)
    }
}

Explanation

Since I'm using Realm in async tasks (because I write on database the results of networking requests), I'm calling initRealm() right before each time I try to access the RealmManager to write or read (in order to avoid exceptions due to uisng a Realm initialized in another thread).

The issue is that, very rarely, when I call initRealm(), something happens with the memory management and the application crashes with an EXC_BAD_ACCESS KERN_INVALID_ADDRESS.

It is theoretically an indicator of a dangling pointer, but I'm puzzled because so far it has only happened really few times (4 times in production environment and 1 time in testing, a long time ago).

Any help or hint you can provide would be very appreciated. Thanks in advance!

Version of Realm and Tooling

iOS versions: 10.0 to 13.1

XCode versions: 10.3 and 11

Realm framework version: Realm (3.17.3)

Dependency manager + version: CocoaPods 1.8.3

@ejm01
Copy link
Contributor

ejm01 commented Apr 7, 2020

Thanks for the details.

In order tell why the KERN_INVALID_ADDRESS threw, we'll ultimately need a reproduction to understand the memory (de)allocation. Have you been able to capture this since you submitted the report?

Any help or hint you can provide would be very appreciated.

In the interim you could handle the error with a try/catch instead of disabling error propagation (try!)

@philipengberg
Copy link

We are experiencing this as well. We are unable to reproduce. It happens for quite a few users, but not everyone. Was introduced when jumping to 4.x.

@KimiChiu
Copy link

KimiChiu commented May 31, 2020

Getting this error 377 times in 7 days after updated to realm 5.0.0. It's a disaster.
All the exceptions were thrown from AppDelegate.supportedInterfaceOrientationsFor and AppDelegate.didFinishLaunchingWithOptions.
Is there a new limitation about when we can call realm after initialization?
And I didn't set encrypted to true, how come it's still doing some encryption?

Crashed: com.apple.main-thread
0  Realm                          0x1046d35a0 realm::util::EncryptedFileMapping::read_barrier(void const*, unsigned long, unsigned long (*)(char const*)) + 32
1  Realm                          0x1043d2154 realm::util::do_encryption_read_barrier(void const*, unsigned long, unsigned long (*)(char const*), realm::util::EncryptedFileMapping*) + 8932
2  Realm                          0x10463c050 long long realm::IndexArray::index_string<(realm::IndexMethod)1>(realm::StringData, realm::InternalFindResult&, realm::ClusterColumn const&) const + 400
3  Realm                          0x10463bed8 realm::IndexArray::index_string_find_all_no_copy(realm::StringData, realm::ClusterColumn const&, realm::InternalFindResult&) const + 24
4  Realm                          0x10467e0d8 realm::StringNode<realm::Equal>::_search_index_init() + 180
5  Realm                          0x10467ddb8 realm::StringNodeEqualBase::init() + 44
6  Realm                          0x104512008 realm::Query::init() const + 64
7  Realm                          0x1045120d8 realm::Query::find_all(realm::ConstTableView&, unsigned long, unsigned long, unsigned long) const + 92
8  Realm                          0x1046c5320 realm::ConstTableView::do_sync() + 424
9  Realm                          0x1045124d0 realm::Query::find_all(unsigned long, unsigned long, unsigned long) + 108
10 Realm                          0x104512950 realm::Query::find_all(realm::DescriptorOrdering const&) + 240
11 Realm                          0x104408e30 realm::Results::do_evaluate_query_if_needed(bool) + 383 (results.cpp:383)
12 Realm                          0x1044089a4 realm::util::Optional<realm::Obj> realm::Results::try_get<realm::Obj>(unsigned long) + 307 (results.cpp:307)
13 Realm                          0x104408c18 realm::util::Optional<realm::Obj> realm::Results::first<realm::Obj>() + 151 (__mutex_base:151)
14 Realm                          0x1044aeb84 RLMAccessorContext realm::Results::dispatch<auto realm::Results::first<RLMAccessorContext>(RLMAccessorContext&)::'lambda'<typename $T>(RLMAccessorContext&)>(RLMAccessorContext&) const + 402 (optional.hpp:402)
15 Realm                          0x1044ac8e4 -[RLMResults firstObject] + 60 (RLMResults_Private.hpp:60)
16 RealmSwift                     0x10497c6dc Results.first.getter + 149 (Results.swift:149)
17 pitt                           0x10285478c UserPreferencesService.getUserPreferences(owner:name:) + 1734 (UserPreferencesService.swift:1734)
18 pitt                           0x10287c768 SingletonProperty.useRecoveryCache.getter + 449 (UserPreferencesService.swift:449)
19 pitt                           0x1029c34f8 specialized AppDelegate.application(_:didFinishLaunchingWithOptions:) + 192 (AppDelegate.swift:192)
20 pitt                           0x1029b74e8 @objc AppDelegate.application(_:didFinishLaunchingWithOptions:) + 4305597672 (<compiler-generated>:4305597672)
21 UIKitCore                      0x23f90880c -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 416
22 UIKitCore                      0x23f909fdc -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3432
23 UIKitCore                      0x23f90f9fc -[UIApplication _runWithMainScene:transitionContext:completion:] + 1584
24 UIKitCore                      0x23f1a1e04 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 800
25 UIKitCore                      0x23f1aa7e0 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 168
26 UIKitCore                      0x23f1a1a6c -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 252
27 UIKitCore                      0x23f1a2388 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1068
28 UIKitCore                      0x23f1a05d0 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 756
29 UIKitCore                      0x23f1a028c -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 460
30 UIKitCore                      0x23f1a4fbc __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 224
31 UIKitCore                      0x23f1a5e14 _performActionsWithDelayForTransitionContext + 120
32 UIKitCore                      0x23f1a4e70 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 260
33 UIKitCore                      0x23f1a9e60 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 364
34 UIKitCore                      0x23f90df38 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 556
35 UIKitCore                      0x23f4f0650 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 376
36 FrontBoardServices             0x214ea9fd0 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 456
37 FrontBoardServices             0x214eb431c __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 284
38 FrontBoardServices             0x214eb3a6c __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 72
39 libdispatch.dylib              0x211ef5888 _dispatch_client_callout + 20
40 libdispatch.dylib              0x211ef8e5c _dispatch_block_invoke_direct + 252
41 FrontBoardServices             0x214ee718c __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 48
42 FrontBoardServices             0x214ee6e08 -[FBSSerialQueue _performNext] + 436
43 FrontBoardServices             0x214ee7404 -[FBSSerialQueue _performNextFromRunLoopSource] + 56
44 CoreFoundation                 0x212476b54 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
45 CoreFoundation                 0x212476ad0 __CFRunLoopDoSource0 + 92
46 CoreFoundation                 0x21247638c __CFRunLoopDoSources0 + 180
47 CoreFoundation                 0x212471060 __CFRunLoopRun + 988
48 CoreFoundation                 0x212470964 CFRunLoopRunSpecific + 452
49 GraphicsServices               0x2146b1d8c GSEventRunModal + 108
50 UIKitCore                      0x23f911758 UIApplicationMain + 216
51 pitt                           0x1026d4a40 main + 27 (AnsiColor.swift:27)
52 libdyld.dylib                  0x211f2cfd8 start + 4

@philipengberg
Copy link

philipengberg commented Jun 15, 2020

Still happens on 5.x. Also happens on 3.x.

Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x1a6762f14 class_getSuperclass + 4
1  Realm                          0x103b5d63c RLMIsObjectSubclass + 68
2  Realm                          0x103bd8898 RLMRegisterClassLocalNames(objc_class**, unsigned long) + 144
3  Realm                          0x103bd90e4 +[RLMSchema sharedSchema] + 104
4  Realm                          0x103bcc190 +[RLMRealm realmWithConfiguration:queue:error:] + 1648
5  RealmSwift                     0x1043a25d4 $s10RealmSwift0A0V5queueACSo012OS_dispatch_C0CSg_tKcfC + 104
6  Undo                           0x100d4701c TripLogger.init() + 24 (TripLogger.swift:24)
7  Undo                           0x100d46f90 globalinit_33_D65FF9833DF9D60C366AB458D3985695_func164 + 20 (TripLogger.swift:20)
8  libdispatch.dylib              0x1a6707524 _dispatch_client_callout + 16
9  libdispatch.dylib              0x1a66da364 _dispatch_once_callout + 28
10 libswiftCore.dylib             0x1b41aaae4 swift_once + 44
11 Undo                           0x100c7decc TravelInsuranceManager.handleRegisteredVisit(visit:) + 20 (TripLogger.swift:20)
12 Undo                           0x100c80044 partial apply + 4371464260 (<compiler-generated>:4371464260)
13 RxSwift                        0x1048252cc $s7RxSwift14ObservableTypePAAE9subscribe6onNext0F5Error0F9Completed0F8DisposedAA10Disposable_py7ElementQzcSg_ys0H0_pcSgyycSgAOtFyAA5EventOyAKGcfU_ + 340
14 RxSwift                        0x104825510 $s7RxSwift14ObservableTypePAAE9subscribe6onNext0F5Error0F9Completed0F8DisposedAA10Disposable_py7ElementQzcSg_ys0H0_pcSgyycSgAOtFyAA5EventOyAKGcfU_TA + 44
15 RxSwift                        0x1047dd5c0 $s7RxSwift17AnonymousObserverC6onCoreyyAA5EventOyxGF + 20
16 RxSwift                        0x104827a68 $s7RxSwift12ObserverBaseC2onyyAA5EventOyxGF + 276
17 RxSwift                        0x104827bac $s7RxSwift12ObserverBaseCyxGAA0C4TypeA2aEP2onyyAA5EventOy7ElementQzGFTW + 20
18 RxSwift                        0x104827fbc $sTA + 76
19 RxSwift                        0x10483f424 $s7RxSwift5EventOyxGIegn_ADytIegnr_7ElementQyd__RszAA12ObserverTypeRd__r__lTR + 20
20 RxSwift                        0x10482da14 $s7RxSwift14PublishSubjectC2onyyAA5EventOyxGF + 76
21 RxSwift                        0x10482eb3c $s7RxSwift14PublishSubjectCyxGAA12ObserverTypeA2aEP2onyyAA5EventOy7ElementQzGFTW + 20
22 RxSwift                        0x104827d50 $s7RxSwift12ObserverTypePAAE6onNextyy7ElementQzF + 188
23 Undo                           0x100d0698c closure #1 in LocationManager.locationManager(_:didVisit:) + 281 (LocationManager.swift:281)
24 Undo                           0x1012fe45c thunk for @escaping @callee_guaranteed (@guaranteed [CLPlacemark]?, @guaranteed Error?) -> () + 4378272860 (<compiler-generated>:4378272860)
25 libdispatch.dylib              0x1a67069a8 _dispatch_call_block_and_release + 24
26 libdispatch.dylib              0x1a6707524 _dispatch_client_callout + 16
27 libdispatch.dylib              0x1a66ea6fc _dispatch_main_queue_callback_4CF$VARIANT$armv81 + 860
28 CoreFoundation                 0x1a69bf748 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
29 CoreFoundation                 0x1a69ba61c __CFRunLoopRun + 1724
30 CoreFoundation                 0x1a69b9c34 CFRunLoopRunSpecific + 424
31 GraphicsServices               0x1b0b0338c GSEventRunModal + 160
32 UIKitCore                      0x1aaaec22c UIApplicationMain + 1932
33 Undo                           0x100bcf880 main + 23 (AppDelegate.swift:23)
34 libdyld.dylib                  0x1a6841800 start + 4

@sync-by-unito
Copy link

sync-by-unito bot commented Mar 24, 2021

➤ Brian Munkholm commented:

This is assumed Fixed a while back now. If you are still seeing this, please comment with fresh observations.

@sync-by-unito sync-by-unito bot closed this as completed Mar 24, 2021
@mr-grey
Copy link

mr-grey commented Aug 17, 2022

The same crash: in start application
Hardware Model: iPhone10,6
OS Version: iPhone OS 15.6 (19G71)

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000000c300000a
Exception Codes: 0x0000000000000001, 0x00000000c300000a
VM Region Info: 0xc300000a is not in any region. Bytes before following region: 1061486582
REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL
UNUSED SPACE AT START
--->
__TEXT 102450000-102454000 [ 16K] r-x/r-x SM=COW ...235/Me.app/Me
Exception Note: EXC_CORPSE_NOTIFY
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [22890]

Thread 14 name:
Thread 14 Crashed:
0 libobjc.A.dylib 0x00000001986372bc class_getSuperclass + 4 (objc-runtime-new.h:1896)
1 Realm 0x0000000108f68eec RLMIsObjectSubclass + 20 (RLMObject.mm:253)
2 Realm 0x0000000109046340 RLMRegisterClassLocalNames(objc_class**, unsigned long) + 128 (RLMSchema.mm:131)
3 Realm 0x0000000109046ba8 +[RLMSchema sharedSchema] + 104 (RLMSchema.mm:295)
4 Realm 0x0000000109038b30 +[RLMRealm realmWithConfiguration:queue:error:] + 1580 (RLMRealm.mm:560)
5 RealmSwift 0x00000001088923c8 $sSo8RLMRealmC13configuration5queueABSo0A13ConfigurationC_So012OS_dispatch_C0CSgtKcfCTOTf4ggn_n + 40 (:0)
6 RealmSwift 0x00000001088923c8 $sSo8RLMRealmC13configuration5queueABSo0A13ConfigurationC_So012OS_dispatch_C0CSgtKcfCTO + 40 (:0)
7 RealmSwift 0x00000001088923c8 Realm.init(configuration:queue:) + 92 (Realm.swift:95)
8 Me 0x00000001028a08d4 closure #3 in static RealmManager.initRealm() + 328 (RealmManager.swift:263)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants