【发布时间】:2019-09-12 07:14:43
【问题描述】:
我有一个包含 Swift 和 Objective-C 代码的混合应用程序。在 Xcode 9.4 中运行代码时没有问题。升级到 Xcode 11(加上请求升级到 Swift 4.2)后,应用程序在尝试引用 Realm 0.93.2 库中已释放的实例时崩溃并出现 EXC_BAD_ACCESS 错误。
以下是事件的顺序:
- 启动:
@UIApplicationMain
class VGPAppDelegate: UIResponder, UIApplicationDelegate, BITCrashManagerDelegate, BITHockeyManagerDelegate {
var window: UIWindow?
internal func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//trigger the ui appearance
VGPUIAppearance.triggerAppearance()
//setup network monitoring
TSReachabilityManager.default()
//Update Realm Scheme
VGPRealmMigrator.updateRealmSchema()
setUpHockeyApp()
setupAppearance()
if didCrashInLastSessionOnStartup() {
// Delay launch for sending the crash report
} else {
setupApplication()
}
return true
}
- 在调用 VGPRealmMigrator.updateRealmSchema() 之后
for (Class cls in s_localNameToClass.allValues) {
RLMObjectSchema *schema = [RLMObjectSchema
schemaForObjectClass:cls];
[schemaArray addObject:schema];
// override sharedSchema classs methods for performance
RLMReplaceSharedSchemaMethod(cls, schema);
// set standalone class on shared shema for standalone object creation
schema.standaloneClass = RLMStandaloneAccessorClassForObjectClass(schema.objectClass, schema);
}
free(classes);
注意:架构在 Realm > RLMSchema.mm > Intitialise 中有一个有效的指针
- 现在在 Realm > RLMAccessor.mm
static Class RLMCreateAccessorClass(Class objectClass,
RLMObjectSchema *schema,
NSString *accessorClassPrefix,
IMP (*getterGetter)(RLMProperty *, RLMAccessorCode, NSString *),
IMP (*setterGetter)(RLMProperty *, RLMAccessorCode)) {
// throw if no schema, prefix, or object class
if (!objectClass || !schema || !accessorClassPrefix) {
@throw RLMException(@"Missing arguments");
}
if (!RLMIsKindOfClass(objectClass, RLMObjectBase.class)) {
@throw RLMException(@"objectClass must derive from RLMObject or Object");
}
// create and register proxy class which derives from object class
NSString *accessorClassName = [accessorClassPrefix stringByAppendingString:schema.className];
Class accClass = objc_getClass(accessorClassName.UTF8String);
if (!accClass) {
accClass = objc_allocateClassPair(objectClass, accessorClassName.UTF8String, 0);
objc_registerClassPair(accClass);
}
应用程序在包含引用 schema.className 的语句中崩溃。这里模式有空指针。 Zombie 显示“消息已发送到已释放的实例”。
【问题讨论】: