Iphone 被弧弄糊涂了

Iphone 被弧弄糊涂了,iphone,xcode,automatic-ref-counting,Iphone,Xcode,Automatic Ref Counting,下面的代码是我在Xcode 3.2上使用的,效果非常好 @interface MoObject : UIViewController { NSMutableArray *categoryArray; } @property (nonatomic, retain) NSMutableArray *categoryArray; @end; @implementation MyObject @synthesize categoryArray; - (void)

下面的代码是我在Xcode 3.2上使用的,效果非常好

@interface MoObject : UIViewController 
{

    NSMutableArray *categoryArray;     
}



@property (nonatomic, retain) NSMutableArray *categoryArray;

@end;



@implementation MyObject

@synthesize categoryArray;


- (void)viewDidLoad
{
     [super viewDidLoad];


    NSMutableArray *a = [[NSMutableArray alloc] init];

    categoryArray = a;
  [a  release]; //AAA:I can not release this, it does not report compiling error , but after some operation, it will cause exec bad error

}
- (void)dealloc {

    [category release];

    [super dealloc];
}
我刚转到Xcode 4.3.1 iOS 5.1

同一个函数会导致如此多的exec错误

即使我关闭整个项目的弧

问题仍然存在,看起来我无法在AAA点释放对象数组


欢迎发表评论。关键是它可以为您处理保留/发布代码。由于数组a是在本地声明的,因此当该函数结束时,ARC将插入代码并为您释放它。dealloc也一样-ARC不需要它,实际上也不能使用它


但如果你不喜欢或不想学习ARC,你就不必使用它。这是创建新项目时的一个选项。

您没有使用ARC。如果是,使用-release的代码甚至不会编译。在-dealloc方法中调用[super dealloc]也是如此

您所遇到的只是一个普通的内存管理错误。您正在将直接分配给实例变量categoryArray。您使用的不是为属性合成的setter,而是执行适当内存管理的setter。因此,categoryArray最终指向一个解除分配的对象。如果您使用self.categoryArray=a或[self-setCategoryArray:a]这两种等效语句,那么您的bug就会消失


在ARC下,这个bug大部分是隐藏的。categoryArray实例变量默认为_强,因此至少只要categoryArray指向该数组,该数组就会被保留,并且不会出现EXC_BAD_访问错误。但是,除了在初始值设定方法中,直接分配给它(绕过setter)仍然有问题。

为什么要在dealloc中释放categoryArray?它被保留在任何地方吗?这可能就是问题所在。@property非原子,retain NSMutableArray*categoryArray;我试着关闭arc跟踪:但看起来arc仍然有效!