问题:
我有一个__unsafe_unretained id
指向已发布对象的指针。到目前为止一切顺利,只要我根本不“使用”指针(特别是,我不通过指针调用任何方法)。但是,当我尝试从方法返回其值时,它会崩溃,即使我已明确指定返回值具有 type __unsafe_unretained id
。这是为什么?我想如果我使用__unsafe_unretained
,它根本不会调用retain
/ release
/之类的方法autorelease
吗?我想我可以使用__unsafe_unretained id
它,就好像它是一个void*
(意味着它只做简单的本地作业)?
环境:
- 发展于
Xcode 4.4.1
- 使用
iOS SDK 5.1
ARC
是enabled
- 运行
iPhone 4.3 / 5.0 / 5.1 Simulator
或iPhone 4.3 Device
- 两者都崩溃
Debug
并Release
构建
源代码:
// Declare my class with 1 member.
@interface MyClass : NSObject
{
__unsafe_unretained id m_MyMember;
}
@end
// **************************************************************************************************** //
// Implement my class.
@implementation MyClass
// Setter
-(void)SetMember:(__unsafe_unretained id)member
{
m_MyMember = member;
}
// Getter: by passing parameter by reference
-(void)GetMember1:(__unsafe_unretained id*)member
{
*member = m_MyMember; // No problem.
}
// Getter: by return value
-(__unsafe_unretained id)GetMember2
{
return m_MyMember; // Crashed in here!
}
@end
// **************************************************************************************************** //
//! Application entry point.
int main(int argc, char *argv[])
{
@autoreleasepool
{
{
// Create an object that dies immediately. deadObj is a dangling pointer.
__unsafe_unretained id deadObj = [[NSMutableString alloc] initWithFormat:@"%d", 12];
// Create my object.
MyClass* myObject = [[MyClass alloc] init];
// Assign my member.
[myObject SetMember:deadObj];
// Get back my member: by passing parameter by reference
__unsafe_unretained id unsafePointer1;
[myObject GetMember1:&unsafePointer1]; // No problem.
// Get back my member: by return value
__unsafe_unretained id unsafePointer2;
unsafePointer2 = [myObject GetMember2]; // Crashed in here!
int BreakpointHere = 0;
}
}
}
调用堆栈(iPhone 4.3 模拟器/iOS 4.3 设备):
#0 0x011db09b in objc_msgSend ()
#1 0x00106712 in __arclite_objc_retainAutoreleaseReturnValue at /SourceCache/arclite_host/arclite-29.1/source/arclite.m:259
#2 0x00001fec in -[MyClass GetMember2] at /Users/user/SourceCode/main.m:28
#3 0x00002147 in main at /Users/user/SourceCode/main.m:56
调用堆栈(iPhone 5.0/5.1 模拟器):
#0 0x014f6d25 in objc_retain ()
#1 0x014f7fe3 in objc_retainAutoreleaseReturnValue ()
#2 0x00001fec in -[MyClass GetMember2] at /Users/user/SourceCode/main.m:28
#3 0x00002147 in main at /Users/user/SourceCode/main.m:56