realm-swift: Realm crash on Results.first.getter

Goals

Realm won’t crash

Expected Results

The crash occurs when calling first on a Realm Results object. I’m guessing this is a concurrency issue, but Realm is instantiated on same thread as query, and the Realm objects are immediately converted to app objects (ie., never passed between threads).

Actual Results

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
#0: 0x0000000103bfc2fd Realm`realm::util::EncryptedFileMapping::read_barrier(void const*, unsigned long, unsigned long (*)(char const*)) + 29
  * frame #1: 0x00000001035ce000 Realm`realm::util::do_encryption_read_barrier(addr=0x00007fff87a95540, size=8, header_to_size=(Realm`realm::NodeHeader::get_byte_size_from_header(char const*) at node_header.hpp:201), mapping=0xca4c51df08c18be5)(char const*), realm::util::EncryptedFileMapping*) at file_mapper.hpp:133:14
#2: 0x0000000103b496ee Realm`realm::IndexArray::index_string_all(realm::StringData, std::__1::vector<realm::ObjKey, std::__1::allocator<realm::ObjKey> >&, realm::ClusterColumn const&) const + 398
#3: 0x00000001039f66af Realm`realm::IntegerNode<realm::ArrayIntNull, realm::Equal>::init() + 255
#4: 0x00000001039d1280 Realm`realm::Query::init() const + 48
#5: 0x00000001039d135d Realm`realm::Query::find_all(realm::ConstTableView&, unsigned long, unsigned long, unsigned long) const + 93
#6: 0x0000000103bea7a5 Realm`realm::ConstTableView::do_sync() + 469
#7: 0x00000001039d1801 Realm`realm::Query::find_all(unsigned long, unsigned long, unsigned long) + 97
#8: 0x00000001039d1d02 Realm`realm::Query::find_all(realm::DescriptorOrdering const&) + 258
#9: 0x00000001036baad6 Realm`realm::Results::do_evaluate_query_if_needed(this=0x00007ffe45fd7c18, wants_notifications=true) at results.cpp:383:40
#10: 0x00000001036ba3cc Realm`realm::util::Optional<realm::Obj> realm::Results::try_get<realm::Obj>(this=0x00007ffe45fd7c18, row_ndx=0) at results.cpp:306:13
#11: 0x00000001036ba7ab Realm`realm::util::Optional<realm::Obj> realm::Results::first<realm::Obj>(this=0x00007ffe45fd7c18) at results.cpp:330:12
#12: 0x000000010386fa64 Realm`_ZZN5realm7Results5firstI18RLMAccessorContextEEDaRT_ENKUlTyS4_E_clIPNS_3ObjEEES3_S4_(this=0x00007ffeed6bb2e0, t=0x0000000000000000) at results.hpp:344:28
#13: 0x000000010386eb50 Realm`_ZN5realmL14switch_on_typeINS_3ObjEZNS_7Results5firstI18RLMAccessorContextEEDaRT_EUlTyS6_E_EES5_NS_12PropertyTypeEOT0_(type=Object, fn=0x00007ffeed6bb2e0) at property.hpp:202:33
#14: 0x000000010386e6c3 Realm`_ZNK5realm7Results8dispatchIZNS0_5firstI18RLMAccessorContextEEDaRT_EUlTyS5_E_EES4_OS5_(this=0x00007ffe45fd7c18, fn=0x00007ffeed6bb2e0) at results.hpp:330:12
#15: 0x000000010386e67c Realm`auto realm::Results::first<RLMAccessorContext>(this=0x00007ffe45fd7c18, ctx=0x00007ffeed6bb3b8) at results.hpp:343:12
#16: 0x000000010386e649 Realm`-[RLMResults firstObject]::$_8::operator(this=0x00007ffeed6bb3a8)() const at RLMResults.mm:228:25
#17: 0x0000000103869d46 Realm`auto translateRLMResultsErrors<-[RLMResults firstObject]::$_8>(f=0x00007ffeed6bb3a8, aggregateMethod=0x0000000000000000) at RLMResults_Private.hpp:60:16
#18: 0x0000000103869cbf Realm`-[RLMResults firstObject](self=0x00007ffe45fd7c10, _cmd="firstObject") at RLMResults.mm:227:12
#19: 0x0000000104d7c973 RealmSwift`Results.first.getter(self=RealmSwift.Results<_Ad_Hoc_Test.PersistentTag> @ 0x00007ffeed6bb4f0) at Results.swift:149:52
#20: 0x00000001026869ba  Ad Hoc Test`static LibraryManager.LibraryPersistence.findPersistentTag(permanentId=69, temporaryId="F3D5F66D-0492-43A1-A030-AEC73FAF90CE", self=_Ad_Hoc_Test.LibraryManager.LibraryPersistence) at LibraryManager.swift:822:139
#21: 0x0000000102686458  Ad Hoc Test`LibraryManager.getTag(assignment=_Ad_Hoc_Test.TagAssignment @ 0x00007ffeed6bb960, self=0x0000600002e96610) at LibraryManager.swift:54:38
#22: 0x0000000102606298  Ad Hoc Test`closure #1 in AudiobookUserSettings.tags.getter(assignment=_Ad_Hoc_Test.TagAssignment @ 0x00007ffeed6bb960) at AudiobookUserSettings.swift:22:43
#23: 0x000000010509f72d libswiftCore.dylib`(extension in Swift):Swift.Sequence.compactMap<A>((A.Element) throws -> Swift.Optional<A1>) throws -> Swift.Array<A1> + 733
#24: 0x00000001026061ee  Ad Hoc Test`AudiobookUserSettings.tags.getter(self=0x000060000037b750) at AudiobookUserSettings.swift:21:31
#25: 0x000000010258be66  Ad Hoc Test`Audiobook.tags.getter(self=0x00007ffe45e23b80) at Audiobook.swift:34:35
#26: 0x00000001025a792c  Ad Hoc Test`AudiobookDetailViewController.updateAudiobookStateDetails(self=0x00007ffe46028a00) at AudiobookDetailViewController.swift:379:77
#27: 0x00000001025a5e5b  Ad Hoc Test`AudiobookDetailViewController.updateAudiobookDetails(self=0x00007ffe46028a00) at AudiobookDetailViewController.swift:354:13
#28: 0x00000001025a0987  Ad Hoc Test`closure #1 in AudiobookDetailViewController.libraryChanged(self=0x00007ffe46028a00) at AudiobookDetailViewController.swift:191:18
#29: 0x0000000102543990  Ad Hoc Test`thunk for @escaping @callee_guaranteed () -> () at <compiler-generated>:0
#30: 0x0000000105b5ef11 libdispatch.dylib`_dispatch_call_block_and_release + 12
#31: 0x0000000105b5fe8e libdispatch.dylib`_dispatch_client_callout + 8
#32: 0x0000000105b6dd97 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1149
#33: 0x00007fff23da0909 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
#34: 0x00007fff23d9b459 CoreFoundation`__CFRunLoopRun + 2041
#35: 0x00007fff23d9a944 CoreFoundation`CFRunLoopRunSpecific + 404
#36: 0x00007fff38ba6c1a GraphicsServices`GSEventRunModal + 139
#37: 0x00007fff48c8b9ec UIKitCore`UIApplicationMain + 1605
#38: 0x00000001027d1aef  Ad Hoc Test`main at main.swift:44:1
#39: 0x00007fff51a231fd libdyld.dylib`start + 1## Steps for others to Reproduce

Don’t know how to repro

Code Sample

    func getTag(forAssignment assignment: TagAssignment) -> Tag? {
        let tag = LibraryPersistence.findPersistentTag(permanentId: assignment.permanentTagId,
                                                       temporaryId: assignment.temporaryTagId)
        return tag?.toTag()
    }

    static func findPersistentTag(permanentId: Int? = nil, temporaryId: String? = nil) -> PersistentTag? {
        guard let userId = getIdForCurrentUser() else {
            return nil
        }
        let realm = try! Realm()
        // First try lookup by permanent tag id
        if let permanentId = permanentId {
            let permanentIdNumber = NSNumber(value: permanentId)                
            if let stored = realm.objects(PersistentTag.self).filter("permanentId == %@ AND userId == %@", permanentIdNumber, userId).first {
                return stored
            }
        }
        // Then try temporary tag id
        if let temporaryId = temporaryId {
            return realm.objects(PersistentTag.self).filter("temporaryId == %@ AND userId == %@", temporaryId, userId).first
        }
        return nil
    }

Version of Realm and Tooling

ProductName:	Mac OS X
ProductVersion:	10.15.2
BuildVersion:	19C57

/Applications/Xcode11.5.app/Contents/Developer
Xcode 11.5
Build version 11E608c

/usr/local/bin/pod
1.9.3
Realm (5.3.2)
RealmSwift (5.3.2)
RealmSwift (~> 5.0)

/bin/bash
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)


(not in use here)

/usr/bin/git
git version 2.24.3 (Apple Git-128)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

@sipersso If you’re still up for it, please let me know if you’ve sent me an email by giving a thumbsup so I can look for it.

Thanks @ericjordanmossman, I experimented a bit w/ your sample project but have been unable to repro crash. Will let you know if I succeed.