gpt4 book ai didi

ios - 无法确定 EXC_BAD_ACCESS 的可能原因(在应用程序启动期间发生)

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:41 24 4
gpt4 key购买 nike

我在应用商店的应用程序中看到很多崩溃,我完全无法重现或从代码审查中推测可能是什么原因。特别令人困惑的是,这是在应用程序启动时发生的,而内存不足 - 令人困惑是因为如果没有足够的内存供应用程序启动,我可能希望看到其他类型的崩溃报告而不是 EXC_BAD_ACCESS - 即如果 EXC_BAD_ACCESS是由访问已删除的内存引起的,代码中没有任何地方有机会被删除,因为崩溃是在对象正在创建的过程中发生的。

我已经尝试了很多方法来重现这一点——在模拟器中运行并发送低内存警告消息,在应用程序创建过程中故意使各种对象的初始化失败,在应用程序启动期间调用 calloc 以消耗大量内存,运行该应用程序在 RAM 已完全用尽的手机上多次运行。我无法重新创建,也无法从代码检查中看出可能出了什么问题。

这是一个崩溃报告:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV) Exception Subtype:
KERN_INVALID_ADDRESS at 0x0000000000000000 **(many also at 0x0000000000000008)**
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 The_Name_Of_MyApp 0x000000010268ca94 SearchViewController.init(coder:) + 838292 (SearchViewController.swift:67)
1 The_Name_Of_MyApp 0x000000010268ca88 SearchViewController.init(coder:) + 838280 (SearchViewController.swift:67)
2 The_Name_Of_MyApp 0x000000010268cd4c @objc SearchViewController.init(coder:) + 838988 (SearchViewController.swift:0)
3 UIKitCore 0x00000001bca63d64 -[UIClassSwapper initWithCoder:] + 248
4 UIFoundation 0x000000019a77fe88 UINibDecoderDecodeObjectForValue + 744
5 UIFoundation 0x000000019a7800cc UINibDecoderDecodeObjectForValue + 1324
6 UIFoundation 0x000000019a71eb3c -[UINibDecoder decodeObjectForKey:] + 320
7 UIKitCore 0x00000001bd0a1288 -[UIViewController initWithCoder:] + 1420
8 UIKitCore 0x00000001bceb5dcc -[UITabBarController initWithCoder:] + 80
9 The_Name_Of_MyApp 0x000000010283f658 TabBarViewController.init(coder:) + 2618968 (TabBarViewController.swift:0)
10 The_Name_Of_MyApp 0x000000010283f70c @objc TabBarViewController.init(coder:) + 2619148 (TabBarViewController.swift:0)
11 UIKitCore 0x00000001bca63d64 -[UIClassSwapper initWithCoder:] + 248
12 UIFoundation 0x000000019a77fe88 UINibDecoderDecodeObjectForValue + 744
13 UIFoundation 0x000000019a71eb3c -[UINibDecoder decodeObjectForKey:] + 320
14 UIKitCore 0x00000001bcd9dcbc -[UIRuntimeConnection initWithCoder:] + 188
15 UIFoundation 0x000000019a77fe88 UINibDecoderDecodeObjectForValue + 744
16 UIFoundation 0x000000019a7800cc UINibDecoderDecodeObjectForValue + 1324
17 UIFoundation 0x000000019a71eb3c -[UINibDecoder decodeObjectForKey:] + 320
18 UIKitCore 0x00000001bccc4a1c -[UINib instantiateWithOwner:options:] + 1124
19 UIKitCore 0x00000001bce92f18 -[UIStoryboard instantiateViewControllerWithIdentifier:] + 196
20 The_Name_Of_MyApp 0x000000010266b4dc InitialViewController.start() + 701660 (InitialViewController.swift:118)
21 The_Name_Of_MyApp 0x000000010266bba4 @objc InitialViewController.start() + 703396 (InitialViewController.swift:0)
22 Foundation 0x0000000190bff6d0 __NSFireDelayedPerform + 404
23 CoreFoundation 0x00000001900f2bf0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 28
24 CoreFoundation 0x00000001900f2920 __CFRunLoopDoTimer +864
25 CoreFoundation 0x00000001900f2154 __CFRunLoopDoTimers + 248
26 CoreFoundation 0x00000001900ed030 __CFRunLoopRun + 1880
27 CoreFoundation 0x00000001900ec5b8 CFRunLoopRunSpecific + 436
28 GraphicsServices 0x0000000192360584 GSEventRunModal + 100
29 UIKitCore 0x00000001bc9e3558 UIApplicationMain + 212
30 The_Name_Of_MyApp 0x00000001026ea6b0 main + 1222320 (AppDelegate.swift:20)
31 libdyld.dylib 0x000000018fbacb94 start + 4

Thread 1:
0 libsystem_pthread.dylib 0x000000018fd80cfc start_wqthread + 0

Thread 2 name: Dispatch queue: com.myQueue.backgroundSerialQueue
Thread 2:
0 libsystem_c.dylib 0x000000018fc60ae4 __v2printf + 616
1 libsystem_c.dylib 0x000000018fc60a00 __v2printf + 388
2 libsystem_c.dylib 0x000000018fc46ac0 _vsnprintf + 264
3 libsystem_c.dylib 0x000000018fc3b9c0 snprintf_l + 28
4 CoreFoundation 0x000000019010fa88 __CFStringAppendFormatCore + 8548
5 CoreFoundation 0x00000001901111b8 _CFStringCreateWithFormatAndArgumentsAux2 + 136
6 Foundation 0x0000000190afd3ec -[NSString initWithFormat:locale:] + 36
7 Foundation 0x0000000190afd364 -[NSNumber descriptionWithLocale:] + 1044
8 CoreFoundation 0x000000019004d9a8 -[__NSCFNumber descriptionWithLocale:] + 72
9 CoreFoundation 0x0000000190053268 -[__NSCFNumber stringValue] + 64
10 The_Name_Of_MyApp 0x00000001025f6494 -[RobocallerManager readCityAndStateData] + 222356 (RobocallerManager.m:596)
11 The_Name_Of_MyApp 0x00000001025f343c __25-[RobocallerManager init]_block_invoke + 209980 (RobocallerManager.m:148)
12 libdispatch.dylib 0x000000018fb9b6c8 _dispatch_call_block_and_release + 24
13 libdispatch.dylib 0x000000018fb9c484 _dispatch_client_callout + 16
14 libdispatch.dylib 0x000000018fb43c18 _dispatch_lane_serial_drain$VARIANT$mp + 592
15 libdispatch.dylib 0x000000018fb44760 _dispatch_lane_invoke$VARIANT$mp + 432
16 libdispatch.dylib 0x000000018fb4cf00 _dispatch_workloop_worker_thread + 600
17 libsystem_pthread.dylib 0x000000018fd7e0f0 _pthread_wqthread + 312
18 libsystem_pthread.dylib 0x000000018fd80d00 start_wqthread + 4

Thread 3:
0 libsystem_pthread.dylib 0x000000018fd80cfc start_wqthread + 0

Thread 4 name: com.apple.uikit.eventfetch-thread
Thread 4:
0 libsystem_kernel.dylib 0x000000018fceded0 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x000000018fced3a8 mach_msg + 72
2 CoreFoundation 0x00000001900f1fb0 __CFRunLoopServiceMachPort + 236
3 CoreFoundation 0x00000001900ece4c __CFRunLoopRun + 1396
4 CoreFoundation 0x00000001900ec5b8 CFRunLoopRunSpecific + 436
5 Foundation 0x0000000190ae06a4 -[NSRunLoop+ 34468 (NSRunLoop) runMode:beforeDate:] + 300
6 Foundation 0x0000000190ae0514 -[NSRunLoop+ 34068 (NSRunLoop) runUntilDate:] + 88
7 UIKitCore 0x00000001bc940ac0 -[UIEventFetcher threadMain] + 136
8 Foundation 0x0000000190c133b0 __NSThread__start__ + 1040
9 libsystem_pthread.dylib 0x000000018fd7d2fc _pthread_body + 128
10 libsystem_pthread.dylib 0x000000018fd7d25c _pthread_start + 48
11 libsystem_pthread.dylib 0x000000018fd80d08 thread_start + 4

Thread 5 name: com.twitter.crashlytics.ios.MachExceptionServer
Thread 5:
0 libsystem_kernel.dylib 0x000000018fceded0 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x000000018fced3a8 mach_msg + 72
2 The_Name_Of_MyApp 0x000000010288d08c CLSMachExceptionServer + 1180
3 libsystem_pthread.dylib 0x000000018fd7d2fc _pthread_body + 128
4 libsystem_pthread.dylib 0x000000018fd7d25c _pthread_start + 48
5 libsystem_pthread.dylib 0x000000018fd80d08 thread_start + 4

Thread 6:
0 libsystem_pthread.dylib 0x000000018fd80cfc start_wqthread + 0

Thread 7:
0 libsystem_pthread.dylib 0x000000018fd80cfc start_wqthread + 0

Thread 8:
0 libsystem_pthread.dylib 0x000000018fd80cfc start_wqthread + 0

Thread 9:
0 libsystem_pthread.dylib 0x000000018fd80cfc start_wqthread + 0

Thread 10 name: com.apple.NSURLConnectionLoader
Thread 10:
0 libsystem_kernel.dylib 0x000000018fceded0 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x000000018fced3a8 mach_msg + 72
2 CoreFoundation 0x00000001900f1fb0 __CFRunLoopServiceMachPort + 236
3 CoreFoundation 0x00000001900ece4c __CFRunLoopRun + 1396
4 CoreFoundation 0x00000001900ec5b8 CFRunLoopRunSpecific + 436
5 CFNetwork 0x000000019070ce18 -[__CoreSchedulingSetRunnable runForever] + 212
6 Foundation 0x0000000190c133b0 __NSThread__start__ + 1040
7 libsystem_pthread.dylib 0x000000018fd7d2fc _pthread_body + 128
8 libsystem_pthread.dylib 0x000000018fd7d25c _pthread_start + 48
9 libsystem_pthread.dylib 0x000000018fd80d08 thread_start + 4

Thread 0 crashed with ARM Thread State (64-bit):
x0: 0x0000000000000000 x1: 0x0000000000000000 x2: 0x0000000282718580 x3: 0x0000000000000000
x4: 0x0000000104870680 x5: 0x0000000000000000 x6: 0x0000000000000072 x7: 0x00000000000000a0
x8: 0x0000000103f03650 x9: 0x0000000000000000 x10: 0x000000000000001f x11: 0x0000000000000005
x12: 0x0000000000000005 x13: 0x0000000000000000 x14: 0x0000000000000068 x15: 0x00000001ae696425
x16: 0x000000018f34b858 x17: 0x0000000102601328 x18: 0x0000000000000000 x19: 0x0000000104853200
x20: 0x00000001048709c8 x21: 0x0000000280b73880 x22: 0x00000001c92684c0 x23: 0x0000000104870600
x24: 0x0000000281e51ec0 x25: 0x00000001c73e2000 x26: 0x0000000000000002 x27: 0x00000001c73e2000
x28: 0x0000000000000171 fp: 0x000000016d83c0b0 lr: 0x000000010268ca88
sp: 0x000000016d83bcc0 pc: 0x000000010268ca94 cpsr: 0x60000000

下图概述了崩溃报告中显示的对象之间的关系。基本上在应用程序启动时,AppDelegate 会导致 LicenceManger 对象被实例化,这反过来会导致创建 DatabaseManager 和 Database 对象,所有这些都是模型类型类。同时,GUI 正在启动,InitialVC 是一个根 VC,它实例化一个 TabBar VC,这导致 SearchVC 的实例化。SearchVC 访问 DatabaseModel 对象,这就是崩溃发生的地方。

enter image description here

这里是代码(简化为相关部分,注意代码是 Swift 除了 DataBaseManager 是 Ojb-C)

初始 View Controller

////////////////// InitialViewController ////////////////////////////
class InitialViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.perform(#selector(InitialViewController.start), with: nil, afterDelay: 1.5)
}
}

@objc func start() {
let tbvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBarViewControllerID") as! TabBarViewController
navigationController.pushViewController(tbvc, animated: true)
}

SearchViewController 是由于它与 TabBarViewControllerSp 之间存在关系而创建的

搜索 View Controller

///////////////////////// SearchViewController ///////////////////////////////
class SearchViewController: UITableViewController, UISearchBarDelegate {
var searchResultsList = DatabaseManager.instance().database.emptyList() /** line 67 **/

应用代理

////////////////////// App Delegate ///////////////////////////
AppDelegate:didFinishLaunchingWithOptions() {
let _ = LicenseManager.instance
}

许可证管理器

//////////////////////// LicenseManager ////////////////////////
@objc class LicenseManager : NSObject {
@objc open static let instance = LicenseManager()
private let databaseManager = DatabaseManager.instance()
}

override private init() {
super.init()
}

数据库管理器

///////////////////// DatabaseManager ////////////////////////
+ (DatabaseManager*) instance {
static DatabaseManager *theInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^ {
theInstance = [[DatabaseManager alloc] init];
});
return theInstance;
}

@interface DatabaseManager()
@property (strong, nonatomic) Database* _Nonnull database;
@property (strong, nonatomic) NSMutableDictionary* cityAndStateDictionary;
@end


@implementation DatabaseManager
- (id) init {
self = [super init];
if (self)
{
_database = Database.instance;
[_database openOrCreateDatabase];


dispatch_async(robocallerQueue(), ^{
[self readCityAndStateData];
});
}
return self;
}


//Populates a very large dictionary > 100K entries
- (void) readCityAndStateData
{
NSBundle* bundle = [NSBundle bundleForClass:[DatabaseManager class]];
NSString *filePath = [bundle pathForResource:@"CityState" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
Assert(data, @"unable to read CityState.json file");

NSError* __autoreleasing nserror;
NSDictionary* jsonParser = [NSJSONSerialization JSONObjectWithData: data
options: NSJSONReadingAllowFragments
error: &nserror];
if (nserror == nil)
{
self.cityAndStateDictionary = [[NSMutableDictionary alloc] init];
self.stateDictionary = [[NSMutableDictionary alloc] init];
for (NSDictionary *item in jsonParser)
{
NSNumber* num1 = item[@"Code1"];
NSNumber* num2 = item[@"Code2"];
NSString* key = [NSString stringWithFormat:@"%@%@", num1.stringValue, num2.stringValue];
NSString* city = item[@"City"];
NSString* state = item[@"State"];
NSString* value = [NSString stringWithFormat:@"%@, %@", [city capitalizedString], state];
(self.cityAndStateDictionary)[key] = value;

NSString* stateKey = [num1 stringValue];
(self.stateDictionary)[stateKey] = state;
}
self.cityAndStateDataLoaded = TRUE;
}
else
{
Assert(data, @"unable to parse CityState.json file");
}
}

数据库

////////////////////// Database //////////////////////
@objcMembers class Database : NSObject {
open static let instance = Database()
fileprivate var configuration:Realm.Configuration? = nil

override init()
{
}
}


@objc func openOrCreateDatabase() {
configuration = Realm.Configuration(encryptionKey: Database.getKey() as Data)
}

class func getKey() -> NSData {
let string = "AED16D8B-7762-4CBF-A313-48FDCAD51B9C23456754635498123450ka&9dB6t"
let data = string.data(using: String.Encoding.utf8)
return data! as NSData
}

func emptyList() -> Results<Caller> {
let realm = try! Realm(configuration: configuration!)
let mdn = "DoesNotExist"
let searchTerm = "number == " + "\"" + mdn + "\""
let results = realm.objects(Caller.self).filter(searchTerm)
return results
}

来电者

//////////////// Caller ///////////
@objc public class Caller : Object {
@objc dynamic var name:String? = nil
// More simple types, strings, dates, ints etc.
}

最佳答案

僵尸

这不是强制解包的结果,我猜这是故意的,如果无法创建数据库会故意崩溃。 EXC_BAD_ACCESS KERN_INVALID_ADDRESS当你有一个僵尸时发生:代码试图访问释放的内存(类似的崩溃消息 herehere )。

以下是消除僵尸的一般建议:

  • 在您方案的“诊断”选项卡中启用 Xcode 中的僵尸对象
  • 使用 Instruments 在您的项目中寻找僵尸

第67行

罪魁祸首是:DatabaseManager.instance().database.emptyList()

  1. DatabaseManager.instance()

    这是首先在 AppDelegate 中通过 let _ = LicenseManager.instance 创建的.由于应用程序在调用 SearchViewController.init(coder:) 时崩溃,而不是在 AppDelegate 中,可以公平地假设创建 DatabaseManager.instance() , Database.instance在 AppDelegate 中是无害的。

  2. DatabaseManager.instance().database

    这也不是罪魁祸首,如前所述:这是在 AppDelegate 中创建的。

  3. emptyList()

    这给我们留下了这个方法。它在主线程中被调用。作为一种方法本身,它会以 KERN_INVALID_ADDRESS 使应用程序崩溃的唯一情况。消息,如果它被发送到僵尸。这意味着 DatabaseManager.instance().database在主线程中还不存在。这意味着 1.5 InitialViewController 中的第二次延迟不够 robocallerQueue返回一个非零 DatabaseManager.instance().database .从崩溃报告可以看到,当主线程(线程0)崩溃时,线程2还在执行readCityAndStateData .


解决方案

我建议标记 searchResultsList懒惰,或使用完成处理程序而不是 1.5 秒延迟。

关于ios - 无法确定 EXC_BAD_ACCESS 的可能原因(在应用程序启动期间发生),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52545141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com