realm-swift: Crash inside RLMIsObjectSubclass

Goals

Open realm.

Expected Results

No crash

Actual Results

Crash

Crashed: com.apple.root.user-initiated-qos
0  libobjc.A.dylib                0x18235361c class_getSuperclass + 4
1  GalileoPro                     0x1009461d8 RLMIsObjectSubclass (RLMUtil.hpp:70)
2  GalileoPro                     0x1009b84b4 RLMRegisterClassLocalNames(objc_class**, unsigned long) (RLMSchema.mm:104)
3  GalileoPro                     0x1009b8d44 +[RLMSchema sharedSchema] (memory:2803)
4  GalileoPro                     0x1009ae41c +[RLMRealm realmWithConfiguration:error:] (RLMRealm.mm:425)
5  GalileoPro                     0x1008168fc __31-[DataSource initDefaultRealm:]_block_invoke_2 (DataSource.mm:758)
6  libdispatch.dylib              0x182a8aa54 _dispatch_call_block_and_release + 24
7  libdispatch.dylib              0x182a8aa14 _dispatch_client_callout + 16
8  libdispatch.dylib              0x182a97ea4 _dispatch_root_queue_drain + 1032
9  libdispatch.dylib              0x182a97a38 _dispatch_worker_thread3 + 120
10 libsystem_pthread.dylib        0x182d3306c _pthread_wqthread + 1268
11 libsystem_pthread.dylib        0x182d32b6c start_wqthread + 4

More details at: http://crashes.to/s/a2cb0314979

Steps to Reproduce

It’s our top crash now, it’s happened at 0.5% of daily users.

Code Sample

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSError *error = nil;
        [RLMRealm realmWithConfiguration:config error:&error];
        // commented 
    });

Version of Realm and Tooling

Realm framework version: Realm-cocoa 3.1.1

Realm Object Server version: 2.7.2

Xcode version: 9.2

iOS/OSX version: iOS 11 and iOS 10

Dependency manager + version: Cocoapods 1.4.0

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 9
  • Comments: 43 (6 by maintainers)

Most upvoted comments

What helped for us is disable firebase automatic measurements like _app_start, automatic UIViewControllers loading time, and networking monitoring (Firebase performance): Performance.sharedInstance().isInstrumentationEnabled = false

Apparently to implement automatic firebase measurements they had to do a lot of swizzling, and if disabled, the conflict with Realm is resolved.

Hope this helps!

On Tue, Mar 26, 2019 at 2:54 PM Tim Walsh notifications@github.com wrote:

Anyone find a workaround for this issue?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/realm/realm-cocoa/issues/5618#issuecomment-476650484, or mute the thread https://github.com/notifications/unsubscribe-auth/ACf6HVbiG1fDjace7-aJbwjA4VciE8_Cks5vaiZ7gaJpZM4SIRSR .

Facing this crash now. Have u fixed this bug now???

image image

image

There is nothing we can do here without a repro case.

This issue is currently our top crash as well, we’re still experiencing it on v3.6.

Any updates? Starting to see this crash more often

I can easily reproduce this crash when I use Realm in a custom framework :

  1. Create a custom framework (named MyKit.framework) which is added by Cocoapods.
  2. Link the MyKit.framework to MyProject.xcodeproj.
  3. Create a subclass of RLMObject in MyProject (named MyObj).
  4. Init the Realm used RLMRealmConfiguration -setObjectClasses in the AppDelegate -didFinishLaunch (main thread)

Then, it will crash cause the method RLMIsObjectSubclass(), and also log “Can’t add non-Object type ‘xxxxxx’ to a schema.”

Based on the crash log, class_getSuperclass is being called with an invalid value (perhaps a pointer to deallocated memory, or perhaps a pointer that was never valid at all?). The Realm code that calls class_getSuperclass in this case does so on each class returned by objc_copyClassList with virtually no intervening logic. This suggests that the Objective-C runtime itself is returning the invalid Class instance, and Realm just happens to be crashing by virtue of enumerating all Objective-C classes to detect Realm model classes. Given that the crash appears to be caused by invalid state elsewhere in your process, I’m not sure that there’s anything specific that we can do to address this.

My suggestion would be to attempt to reproduce this locally. Guard Malloc or Malloc Scribble could be useful tools in doing this. Once this is accomplished, Malloc Stack Logging would help you determine whether the faulting address was once a valid allocated address, and where it came from. This may help pinpoint what’s gone wrong.

Thanks to @haozhutw –answer link–

After searching for the whole google I came to their comment and it seems to be a fine solutions as checking the logs of the Method :

static void RLMRegisterClassLocalNames(Class *classes, NSUInteger count) 

actually invokes for every class at the time of initialisation of the app.

My app was having several crashes almost for like 1.5% users on a same line of code in pods file of Realm folder which was this :

BOOL RLMIsObjectSubclass(Class klass) {
    return RLMIsKindOfClass(class_getSuperclass(class_getSuperclass(klass)), RLMObjectBase.class);
}

Changing the configurations for the Class’s sharedInstance for the classes Inheriting Object [ RealmSwiftObject ] in swft as :

class DBManager {

        var db: Realm?
        static let sharedInstance = DBManager()
        
        private init() {
            do {
                var config = Realm.Configuration.defaultConfiguration
                config.objectTypes = [DBDataModel.self]
                //self.db  = try? Realm() <-- Instead of this
                self.db = try Realm(configuration: config) <-- Using This
            } catch {
                print("Error occurred \(error)")
            }
        }
        
    }

Here DBDataModel is inherited from Object ( RealmSwiftObject ) i.e. the realm data models

The main motive is clear that Realm should not find multiple classes amongst the app and should stick to particular array of classes provided to it (as suggested in the mentioned answer) so that It wont have an option to execute several times where in some exceptions it gets nil value which makes the app Crash.

Hope This Helps, as I myself has seen no crash of particular reason after this possible fix.