Realm-cocoa: Realm 2.7.0 Crash on Launch

Created on 23 May 2017  ·  6Comments  ·  Source: realm/realm-cocoa

Goals

  • Able to compact using Realm shouldCompactOnLaunch without crash

Expected Results

  • Run compact realm on launch if Realm size goes beyond 100MB

Actual Results

Crashed: com.apple.main-thread
0  Realm                          0x100ed849c realm::Realm::verify_thread() const (execution_context_id.hpp:71)
1  Realm                          0x100ed6438 realm::Realm::compact() (shared_realm.hpp:169)
2  Realm                          0x100ed60b8 realm::Realm::open_with_config(realm::Realm::Config const&, std::__1::unique_ptr<realm::Replication, std::__1::default_delete<realm::Replication> >&, std::__1::unique_ptr<realm::SharedGroup, std::__1::default_delete<realm::SharedGroup> >&, std::__1::unique_ptr<realm::Group, std::__1::default_delete<realm::Group> >&, realm::Realm*) (group_shared_options.hpp:27)
3  Realm                          0x100e260d4 realm::_impl::RealmCoordinator::pin_version(realm::VersionID) (memory:2722)
4  Realm                          0x100e2627c realm::_impl::RealmCoordinator::register_notifier(std::__1::shared_ptr<realm::_impl::CollectionNotifier>) (vector:1606)
5  Realm                          0x100e2fc10 realm::Results::prepare_async() (memory:4604)
6  Realm                          0x100e30094 realm::Results::add_notification_callback(realm::CollectionChangeCallback) & (memory:4301)
7  Realm                          0x100e45208 RLMNotificationToken* RLMAddNotificationBlock<realm::Results>(objc_object*, realm::Results&, void (objc_object*, RLMCollectionChange*, NSError*) block_pointer, bool) (RLMCollection.mm:362)
8  Realm                          0x100ebd424 -[RLMResults addNotificationBlock:] (RLMResults.mm:448)
9  RealmSwift                     0x1012a8500 Results.addNotificationBlock((RealmCollectionChange<Results<A>>) -> ()) -> RLMNotificationToken (Results.swift:378)
10 OneReminder                    0x10006d1bc ORRealmManager.(setup() -> ()).(closure #1) (ORRealmManager.swift:63)
11 libdispatch.dylib              0x18cbae9e0 _dispatch_call_block_and_release + 24
12 libdispatch.dylib              0x18cbae9a0 _dispatch_client_callout + 16
13 libdispatch.dylib              0x18cbb35e8 _dispatch_main_queue_callback_4CF + 996
14 CoreFoundation                 0x18dca50c0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
15 CoreFoundation                 0x18dca2cdc __CFRunLoopRun + 1572
16 CoreFoundation                 0x18dbd2d94 CFRunLoopRunSpecific + 424
17 GraphicsServices               0x18f63c074 GSEventRunModal + 100
18 UIKit                          0x193e8b130 UIApplicationMain + 208
19 OneReminder                    0x10005efac main (main.swift:25)
20 libdyld.dylib                  0x18cbe159c start + 4

Steps to Reproduce

  • After using our app a few days, it starts crashing
  • Crash on every launch

Code Sample

var config = Realm.Configuration(shouldCompactOnLaunch: { totalBytes, usedBytes in
            // totalBytes refers to the size of the file on disk in bytes (data + free space)
            // usedBytes refers to the number of bytes used by data in the file

            // Compact if the file is over 100MB in size and less than 50% 'used'
            let oneHundredMB = 100 * 1024 * 1024
            return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
        })

        config.schemaVersion = currentSchemaVersion

        config.migrationBlock = { (migration, oldSchemaVersion) in
           ...
        }

Realm.Configuration.defaultConfiguration = config

Version of Realm and Tooling


Realm framework version: 2.6.1 / 2.7.0

Realm Object Server version: N/A

Xcode version: 8.3.2

iOS/OSX version: 10.3.1

Dependency manager + version: CocoaPods 1.0.1

T-Bug-Crash

Most helpful comment

2017-06-05 10 47 02
2017-06-05 10 46 38

In the after installing the App, start for the first time, Crash!

All 6 comments

We have a similar situation with two of our apps, each using their own Realm schema.

The crash is EXC_BAD_ACCESS. It occurs when shouldCompactOnLaunch is active (in our testing, we have the block always return true) and we iterate through an RLMResults object.

Thread 1Queue : com.apple.main-thread (serial)
#0 0x0150856c in bool realm::AnyExecutionContextID::contains<std::__1::__thread_id>() const
#1 0x0150842e in realm::Realm::verify_thread() const
#2 0x01504d24 in realm::Realm::compact()
#3 0x0150482a in realm::Realm::open_with_config(realm::Realm::Config const&, std::__1::unique_ptr<realm::Replication, std::__1::default_delete<realm::Replication> >&, std::__1::unique_ptr<realm::SharedGroup, std::__1::default_delete<realm::SharedGroup> >&, std::__1::unique_ptr<realm::Group, std::__1::default_delete<realm::Group> >&, realm::Realm*)
#4 0x013874aa in realm::_impl::RealmCoordinator::pin_version(realm::VersionID)
#5 0x01387ad0 in realm::_impl::RealmCoordinator::register_notifier(std::__1::shared_ptr<realm::_impl::CollectionNotifier>)
#6 0x0139ce76 in realm::Results::update_tableview(bool)
#7 0x0139d4c0 in realm::Results::update_linkview()
#8 0x0139d23a in realm::Results::get(unsigned long)
#9 0x014cc89e in -[RLMResults indexInSource:]::$_15::operator()() const
#10    0x014cb034 in auto translateErrors<-[RLMResults indexInSource:]::$_15>(-[RLMResults indexInSource:]::$_15&&, NSString*)
#11    0x014cafca in ::-[RLMResults indexInSource:](NSUInteger)
#12    0x013d6662 in ::-[RLMFastEnumerator countByEnumeratingWithState:count:](NSFastEnumerationState *, NSUInteger)
#13    0x014c73b4 in ::-[RLMResults countByEnumeratingWithState:objects:count:](NSFastEnumerationState *, id *, NSUInteger)
#14    0x01bfbb80 in -[RealmTableViewController realmObjects]
#15    0x01bfbed8 in -[RealmTableViewController tableView:numberOfRowsInSection:]

In the above case, we're getting the referenced realmObjects here:

return [realmReading.trends sortedResultsUsingKeyPath:@“someProperty” ascending:YES];

Followed by self.realmObjects.count to get the row count.

Our other app's crash looks like this:

Thread 1Queue : com.apple.main-thread (serial)
#0 0x014085ac in bool realm::AnyExecutionContextID::contains<std::__1::__thread_id>() const
#1 0x0140846e in realm::Realm::verify_thread() const
#2 0x01404d64 in realm::Realm::compact()
#3 0x0140486a in realm::Realm::open_with_config(realm::Realm::Config const&, std::__1::unique_ptr<realm::Replication, std::__1::default_delete<realm::Replication> >&, std::__1::unique_ptr<realm::SharedGroup, std::__1::default_delete<realm::SharedGroup> >&, std::__1::unique_ptr<realm::Group, std::__1::default_delete<realm::Group> >&, realm::Realm*)
#4 0x012874ea in realm::_impl::RealmCoordinator::pin_version(realm::VersionID)
#5 0x01287b10 in realm::_impl::RealmCoordinator::register_notifier(std::__1::shared_ptr<realm::_impl::CollectionNotifier>)
#6 0x0129ceb6 in realm::Results::update_tableview(bool)
#7 0x0129d2d0 in realm::Results::get(unsigned long)
#8 0x013cc8de in -[RLMResults indexInSource:]::$_15::operator()() const
#9 0x013cb074 in auto translateErrors<-[RLMResults indexInSource:]::$_15>(-[RLMResults indexInSource:]::$_15&&, NSString*)
#10    0x013cb00a in ::-[RLMResults indexInSource:](NSUInteger)
#11    0x012d66a2 in ::-[RLMFastEnumerator countByEnumeratingWithState:count:](NSFastEnumerationState *, NSUInteger)
#12    0x013c73f4 in ::-[RLMResults countByEnumeratingWithState:objects:count:](NSFastEnumerationState *, id *, NSUInteger)
#13    0x025e83dc in specialized NSFastEnumerationIterator.(refresh() -> ()).(closure #1) ()
#14    0x0257bb8c in NSFastEnumerationIterator.next() -> Any? ()
#15    0x01b38d34 in RLMIterator.next() -> A?
#16    0x0004dea8 in RecordingCoordinator.preflightCheck(stopNoteText : String?) -> ()

The Results object lookup:

guard let jobs = realm?.objects(Job.self).sorted(byKeyPath: “someProperty”, ascending: false)
for job in jobs {

The app crashes at the for loop.

I also!

Please share an Xcode project and steps to reproduce that we can follow in order to identify the issue.

2017-06-05 10 47 02
2017-06-05 10 46 38

In the after installing the App, start for the first time, Crash!

@shaysemireg was kind enough to provide a reproducible case via email, which makes the problem very clear. When Realm::open_with_config is called from RealmCoordinator::pin_version, it is passed nullptr for the realm argument. The recently-added compact-on-launch code path incorrectly assumes realm is always non-null.

Was this page helpful?
0 / 5 - 0 ratings