realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

Crash on RLMSchema sharedShema Xcode 15, Below iOS 17 when using with SwiftData

Open yasheedv opened this issue 2 years ago • 11 comments

How frequently does the bug occur?

Always

Description

A App is crashing when running below iOS 17 with the project having SwiftData model The crash is happening inside RLMSchema sharedShema unsigned int numClasses; using malloc_ptr = std::unique_ptr<__unsafe_unretained Class[], decltype(&free)>; malloc_ptr classes(objc_copyClassList(&numClasses), &free);

Tried adding the -Wl,-ld_classic options to the OTHER_LDFLAGS build in the project settings. But same crash is happening.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

  1. Create a new project.
  2. Set Minimum deployment target to iOS 13
  3. Create a SwiftData Model using @available(iOS 17, *) check
  4. Install Realm using SPM
  5. Create a Realm Model
  6. Access the default Realm in AppDelegate didFinishLaunchingWithOptions
  7. Run the application on a device that is below iOS 17
  8. Then the application will crash

Project URL: SwiftDataRealm.zip

Version

v10.43.1

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 16.6.1

Build environment

Xcode version: Xcode 15 Dependency manager and version: SPM

yasheedv avatar Oct 16 '23 08:10 yasheedv

The crash is happening if I add a SwiftData model inside the project.

yasheedv avatar Oct 16 '23 11:10 yasheedv

@yasheedv Has this problem been solved? I also encountered this problem

liujieyuu avatar Mar 07 '24 03:03 liujieyuu

10.48.0 still crashes; RealmWithConfiguration:error: is called when starting.

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

Crash Below iOS17, Xcode 15.2

+ (instancetype)sharedSchema {
    @synchronized(s_localNameToClass) {
        // We replace this method with one which just returns s_sharedSchema
        // once initialization is complete, but we still need to check if it's
        // already complete because it may have been done by another thread
        // while we were waiting for the lock
        if (s_sharedSchemaState == SharedSchemaState::Initialized) {
            return s_sharedSchema;
        }

        if (s_sharedSchemaState == SharedSchemaState::Initializing) {
            @throw RLMException(@"Illegal recursive call of +[%@ %@]. Note: Properties of Swift `Object` classes must not be prepopulated with queried results from a Realm.", self, NSStringFromSelector(_cmd));
        }

        s_sharedSchemaState = SharedSchemaState::Initializing;
        try {
            // Make sure we've discovered all classes
            {
                unsigned int numClasses;
                using malloc_ptr = std::unique_ptr<__unsafe_unretained Class[], decltype(&free)>;
                malloc_ptr classes(objc_copyClassList(&numClasses), &free); # Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
                RLMRegisterClassLocalNames(classes.get(), numClasses);
            }

            [s_localNameToClass enumerateKeysAndObjectsUsingBlock:^(NSString *, Class cls, BOOL *) {
                registerClass(cls);
            }];
        }
        catch (...) {
            s_sharedSchemaState = SharedSchemaState::Uninitialized;
            throw;
        }

        // Replace this method with one that doesn't need to acquire a lock
        Class metaClass = objc_getMetaClass(class_getName(self));
        IMP imp = imp_implementationWithBlock(^{ return s_sharedSchema; });
        class_replaceMethod(metaClass, @selector(sharedSchema), imp, "@@:");

        s_sharedSchemaState = SharedSchemaState::Initialized;
    }

    return s_sharedSchema;
}

showchi avatar Mar 08 '24 04:03 showchi

请问如何解决,有人知道吗

lizp-peng avatar Mar 10 '24 14:03 lizp-peng

Same problem for me. Fatal for our app. Is there a workaround?

jln19 avatar Mar 18 '24 17:03 jln19

Still crashing as of 10.49.1. We wrote a whole new feature for our app using SwiftData and can't release it because Realm crashes on all devices running iOS16 or below.

Line 300 Pods/Realm/Realm/RLMSchema.mm:300 malloc_ptr classes(objc_copyClassList(&numClasses), &free);

jln19 avatar Mar 27 '24 21:03 jln19

You can avoid the call to objc_copyClassList() by explicitly setting objectTypes on your Realm configuration.

tgoyne avatar Mar 27 '24 21:03 tgoyne

You can avoid the call to objc_copyClassList() by explicitly setting objectTypes on your Realm configuration.

@tgoyne Thank you much for this. I'm able to avoid the crash by setting those types.

jln19 avatar Mar 27 '24 21:03 jln19

You can avoid the call to objc_copyClassList() by explicitly setting objectTypes on your Realm configuration.

对我有效

  • (void)initDataBase { [ART_RLM_DataBaseTool RLM_setDataBaseName:@"ART_ALM_DATABASE"]; NSLog(@"数据库初始化 ART_ALM_DATABASE");

    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; config.objectClasses = @[ [ART_RLM_ARTIST class], [ART_RLM_SHIYARTIST_MONITOR class], [ART_RLM_CATEGORY_MONITOR class], [ART_RLM_TOURIST_MONITOR class], [ART_RLM_LOG_MONITOR class]]; // 这里你可以添加更多的模型类 [RLMRealmConfiguration setDefaultConfiguration:config]; }

TomasWell avatar Jun 25 '24 03:06 TomasWell

Is this related to this known issue in objc_getClassList when there are objects that don't exist on lower platform versions? https://github.com/swiftlang/swift/issues/61215

rvenable avatar Aug 15 '24 00:08 rvenable