0

启用 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.

有什么帮助吗?

非常感谢!

4

1 回答 1

1
@interface ChilldClass : ParentClass{

您的问题可能是由ChilldClass(错字?)中的拼写错误引起的

于 2013-05-27T13:11:47.233 回答