【发布时间】:2011-12-28 20:36:51
【问题描述】:
我有一个 Objective-C 程序并且我正在使用 ARC(自动引用计数),它在第 23 行引发了分段错误(参见下面的程序)。
问题 1) 为什么会出现分段错误?
下面是程序:
#import<Foundation/Foundation.h>
@interface Car : NSObject
@property (weak) NSNumber* doors;
@end
@implementation Car
@synthesize doors;
@end
int main()
{
system("clear");
@autoreleasepool
{
Car *car1 = [[Car alloc] init];
printf("1\n");
NSNumber *d1 = [[NSNumber alloc] initWithInteger: 4];
printf("2\n");
car1.doors = d1; //Segmentation fault.. why ?
printf("3\n");
}
printf("---- end\n");
return(0);
}
输出:
1
2
Segmentation fault: 11
【问题讨论】:
-
这给出了 KERN_INVALID_ADDRESS,看起来像是总线错误而不是分段错误。
-
只是好奇,为什么你使用 printf() 而不是 NSLog()?
-
我刚接触 Objective-C,所以我倾向于使用 printf
标签: objective-c segmentation-fault automatic-ref-counting nsnumber weak-references