Engimath
Engimath

Reputation: 524

iOS ARC: unexpected 'dealloc' call

I have a problem with an Objective-C class, when ARC is enabled.

My classes looks like these:

@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

I have another class like this one:

@interface OtherClass : NSObject{
  ParentClass *c;
}
@end

@implementation OtherClass

[...]

-(void)decode{
  c = [[ChildClass alloc] init];

  [c decodeMethod];
}

[...]

@end

As you can see, a ChildClass object is created and stored as an attribute in OtherClass. As long as the OtherClass object is living, the ChildClass object pointed by c should be also living, isn't it? Well, I have a BAD_ACCESS error, because after the ChildClass initialization, and before the decodeMethod is called, the dealloc method in ChildClass is automatically executed.

Why? ARC is enabled, so the dealloc method should be called automatically when the ChildClass object is released, but it shouldn't happen in this moment, because is still pointed with c.

Any help?

Thank you very much!

Upvotes: 0

Views: 167

Answers (1)

Blaine Murray
Blaine Murray

Reputation: 250

@interface ChilldClass : ParentClass{

It's possible your issue is caused by a spelling error in ChilldClass (typo?)

Upvotes: 1

Related Questions