In your method doSomeInit
, your statement _title = [CCLabelBMFont labelWithString:@"Test Label" fntFile:"blah.fnt"];
assigns an autorelease CCLabelBMFont
object to _title
directly, i.e. without using the setter method. Therefore, this object is not retained, and released as soon as the app returns to the main run loop. So when doSomething
executes, this object is released, and you get the EXC_BAD_ACCESS error.
You should use the setter method self._title = [CCLabelBMFont labelWithString:@"Test Label" fntFile:"blah.fnt"];
EXC_BAD_ACCESS on attempting access private CCLabelBMFont member within performselector
-
11-04-2022 - |
Question
I started a non ARC cocos2d project.
It throws error EXC_BAD_ACCESS
while attempting to access private CCLabelBMFont
member within performSelector:withObject:afterDelay:
title
is allocating in doSomeInit
method. But somehow I can't access it in doSomething
method.
What should I do to access its children?
I put a simple example below:
a.h
@interface IntroLayer : CCLayer {
}
@property (nonatomic,strong) CCLabelBMFont *_title;
@end
also tried this... no luck. a.h
@interface IntroLayer : CCLayer {
CCLabelBMFont *_title;
}
a.m
@implementation IntroLayer
@synthesize _title;
.
.
- (void)doSomeInit {
_title = [CCLabelBMFont labelWithString:@"Test Label" fntFile:"blah.fnt"];
[self performSelector:@selector(doSomething) withObject:nil afterDelay:5];
}
- (void)doSomething {
int count = 0;
for (CCSprite* la in _title.children) { //<<<<<EXC_BAD_ACCESS ERROR THROWS HERE!!!
la.opacity = 0;
}
[self addChild: _title];
}
Next relevant Question is this:
I modified
_title = [CCLabelBMFont labelWithString:@"Test Label" fntFile:"blah.fnt"];
to
_title=[CCLabelBMFont labelWithString:@"Test Label" fntFile:"blah.fnt"];
[_title retain]; //this fixes EXC_BAD_ACCESS error.
But:
Question 2: Is retain method leaking any memory?
Maybe I should release it in overidden -(void)onExit
method?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow