启用 ARC 时,我遇到了 Objective-C 类的问题。
我的课程如下所示:
@interface ParentClass : NSObject {
}
-(void)decodeMethod;
@end
@implementation ParentClass
-(void)decodeMethod{
}
@end
@interface ChilldClass : ParentClass{
int *buffer;
}
@end
@implementation ChildClass
-(id)init{
self = [super init];
if(self != nil){
buffer = (int *)malloc(20*sizeof(int));
}
return self;
}
-(void)dealloc{
free(buffer);
}
@end
我有另一个像这样的课程:
@interface OtherClass : NSObject{
ParentClass *c;
}
@end
@implementation OtherClass
[...]
-(void)decode{
c = [[ChildClass alloc] init];
[c decodeMethod];
}
[...]
@end
如您所见,ChildClass
创建了一个对象并将其作为属性存储在OtherClass
. 只要OtherClass
对象是活的,ChildClass
指向的对象c
也应该是活的,不是吗?好吧,我有一个BAD_ACCESS 错误,因为在ChildClass
初始化之后,在decodeMethod
调用之前,里面的dealloc
方法ChildClass
会自动执行。
为什么?ARC
已启用,因此该dealloc
方法应在ChildClass
释放对象时自动调用,但此时不应发生,因为仍用c
.
有什么帮助吗?
非常感谢!