[英]Objective-C EXC_BAD_ACCESS with assign object property
The delegate.deviceToken
expression below sometimes throws a bad pointer dereference, apparently out of objc_retain()
. 下面的
delegate.deviceToken
表达式有时会抛出一个错误的指针取消引用,显然是出于objc_retain()
。
MyWebServices.m: MyWebServices.m:
@implementation MyWebServices
+ (void)initializeWithCompletionBlock:(void (^) (id data))completionBlock withErrorBlock:(void (^)(NSError* error))errorBlock {
AppDelegate* delegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
if (delegate.deviceToken == nil) { // MyWebServices.m:29
...
}
...
}
The AppDelegate declares deviceToken
like so: AppDelegate声明
deviceToken
如下所示:
@property (nonatomic, assign) NSString* deviceToken; // #NotMyCode
Quoth the crash report : 崩溃报告 :
Code Type: ARM-64 (Native)
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000f434c4388
Triggered by Thread: 0
0 libobjc.A.dylib 0x0000000180a940b0 objc_retain + 16 (objc-object.h:341)
1 MyApp 0x0000000100054f98 +[MyWebServices initializeWithCompletionBlock:withErrorBlock:] + 200 (MyWebServices.m:29)
2 MyApp 0x000000010003b97c -[AppDelegate initializeWebServices] + 224 (AppDelegate.m:380)
3 MyApp 0x00000001000b123c __47-[AFNetworkReachabilityManager startMonitoring]_block_invoke + 132 (AFNetworkReachabilityManager.m:199)
The call to -[AppDelegate initializeWebServices]
can come out of the AFNetworkReachabilityManager
code, as it does in this case, or out of my application:didRegisterForRemoteNotificationsWithDeviceToken
. 对
-[AppDelegate initializeWebServices]
的调用可以来自AFNetworkReachabilityManager
代码,就像在这种情况下一样,或者来自我的application:didRegisterForRemoteNotificationsWithDeviceToken
。 My deviceToken
is indeed not initialized in AppDelegate
, and so with it's assign
semantics it seems clear that I am trying to dereference garbage. 我的
deviceToken
确实没有在AppDelegate
初始化,因此使用它的assign
语义,我似乎很清楚我正在尝试取消引用垃圾。 But how does the nil check cause a call to objc_retain
? 但是nil检查如何导致调用
objc_retain
?
Clearly it's worth trying to initialize deviceToken
to nil
, or update its memory management semantics. 显然,值得尝试将
deviceToken
初始化为nil
,或更新其内存管理语义。 Also noteworthy in the code are a few booleans, the intent of which seems to be the protection of calls to initializeWebServices
, and which may be managed poorly. 代码中还值得注意的是一些布尔值,其意图似乎是保护对
initializeWebServices
的调用,并且可能管理不当。
But I have no idea how to reproduce this bug. 但我不知道如何重现这个bug。
The property is mis-declared; 该财产被误认; it should use the
strong
or copy
attribute instead of assign
: 它应该使用
strong
或copy
属性而不是assign
:
@property (nonatomic, strong) NSString* deviceToken;
Using assign
means the object is not being retained correctly, hence the exception. 使用
assign
意味着对象未被正确保留,因此例外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.