gpt4 book ai didi

ios - 内存不足警告会导致iOS5崩溃-unloadViewForced

转载 作者:行者123 更新时间:2023-12-03 17:37:41 27 4
gpt4 key购买 nike

我已经能够在运行iOS5的iOS设备上复制低内存崩溃。每当堆栈跟踪指向同一位置时,我就确切知道问题出在哪里。不幸的是我不知道如何解决它。如下所示,当CatViewController收到内存警告时,将发生崩溃。那时,它的 View 被强制卸载,然后发生崩溃:

0   libobjc.A.dylib                 0x37174f7e objc_msgSend + 22
1 UIKit 0x317eab66 -[UIViewController unloadViewForced:] + 130
2 UIKit 0x31932492 -[UIViewController purgeMemoryForReason:] + 58
3 MyApp 0x001353c0 -[CatViewController didReceiveMemoryWarning] (CatViewController.m:89)
4 Foundation 0x339e34f8 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 12
5 CoreFoundation 0x35178540 ___CFXNotificationPost_block_invoke_0 + 64
6 CoreFoundation 0x35104090 _CFXNotificationPost + 1400
7 Foundation 0x339573e4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 60
8 Foundation 0x33958c14 -[NSNotificationCenter postNotificationName:object:] + 24
9 UIKit 0x318fd26a -[UIApplication _performMemoryWarning] + 74
10 UIKit 0x318fd364 -[UIApplication _receivedMemoryNotification] + 168
11 libdispatch.dylib 0x37ae6252 _dispatch_source_invoke + 510
12 libdispatch.dylib 0x37ae3b1e _dispatch_queue_invoke$VARIANT$up + 42
13 libdispatch.dylib 0x37ae3e64 _dispatch_main_queue_callback_4CF$VARIANT$up + 152
14 CoreFoundation 0x3517f2a6 __CFRunLoopRun + 1262
15 CoreFoundation 0x3510249e CFRunLoopRunSpecific + 294
16 CoreFoundation 0x35102366 CFRunLoopRunInMode + 98
17 GraphicsServices 0x3476d432 GSEventRunModal + 130
18 UIKit 0x31779e76 UIApplicationMain + 1074
19 MyApp 0x00087142 main (main.m:16)
20 MyApp 0x000870c8 start + 32

因此,我认为这与在iOS5上卸载 View 的方式有关。该 View Controller 不会将其自身从任何通知中删除,因此 this solution不起作用。 CatViewController的89行就是这样:
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; //this is line 89
}

而且viewDidUnload看起来像这样:
- (void)viewDidUnload {
[self setBackgroundView:nil];
[self setContentView:nil];
[self setSomeScrollView:nil];
[super viewDidUnload];
}

关于如何优雅地处理而不崩溃的任何想法?

编辑:这是另一个崩溃报告的更多信息:
0   CoreFoundation                  0x351ac88f __exceptionPreprocess + 163
1 libobjc.A.dylib 0x3717a259 objc_exception_throw + 33
2 CoreFoundation 0x351afa9b -[NSObject doesNotRecognizeSelector:] + 175
3 CoreFoundation 0x351ae915 ___forwarding___ + 301
4 CoreFoundation 0x35109650 _CF_forwarding_prep_0 + 48
5 UIKit 0x317eabdf -[UIViewController unloadViewForced:] + 251
6 UIKit 0x31932499 -[UIViewController purgeMemoryForReason:] + 65
7 MyApp 0x000d541b -[CatViewController didReceiveMemoryWarning] (CatViewController.m:96)

最佳答案

您的应用不会因为内存不足而崩溃。在崩溃报告中,您可以看到抛出的异常实际上是[NSObject doesNotRecognizeSelector:]

首先,您应该将那些设置为nil的语句移到didReceiveMemoryWarning;这是首选方法,因为iOS 6不再支持viewDidUnload。

其次,要查找崩溃,请遍历那些将属性设置为nil的语句,并确保没有一个导致崩溃。请注意,如果将那些变量声明为属性,而不仅仅是实例变量,则只有带有这些签名的方法(self set<propertyName>:])。

关于ios - 内存不足警告会导致iOS5崩溃-unloadViewForced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17764366/

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