In my @Monster class, there are 3 different walk animation.
"Monster.h"
@interface Monster : CCSprite{
CCAction *fWalk;
CCAction *bWalk;
CCAction *hWalk;
}
@property (nonatomic, retain) CCAction *fWalk;
@property (nonatomic, retain) CCAction *bWalk;
@property (nonatomic, retain) CCAction *hWalk;
"Monster.m"
+ (id) monsterInit...
{
Monster *sprite = ...// initialization
NSMutableArray *frameArray = [NSMutableArray array];
for ( int i = 0; i < 3; i++ ) {
NSString *fileName = [NSString stringWithFormation:@"%d.png", i];
[frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
}
CCAnimation *walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
self.fWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
[frameArray removeAllObjects];
for ( int i = 3; i < 6; i++ ) {
NSString *fileName = [NSString stringWithFormation:@"%d.png", i];
[frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
}
walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
sprite.bWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
[frameArray removeAllObjects];
for ( int i = 6; i 开发者_运维百科< 9; i++ ) {
NSString *fileName = [NSString stringWithFormation:@"%d.png", i];
[frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
}
walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
sprite.hWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
return sprite;
}
- (void) dealloc
{
[fWalk release];
[bWalk release];
[hWalk release];
[super dealloc];
}
When I run this app with performance tool - Leaks. Instruments display statement which is "CCAnimation *walk...", "self.fWalk...", "walk = ...", "self.bWalk...", "walk = ...", "self.hWalk.." induce memory leaks.
I checked source code about CCAnimation and CCAnimate, they are all "autorelease".I don't know why this leaks happened. Any idea on what to do?
You have to retain the actions if you plan to use them later, because they are autoreleased when created with the actionWithAction method. Like this:
self.fWalk = [[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]] retain];
Otherwise, your monster class doesn't own the actions and they are autoreleased when you leave the init method.
Ok but when you save all animations into an Array like this:
CCAnimation *anim = [CCAnimation animationWithSpriteFrames:animFrames delay:pDelay];
CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCCallFunc *callback = [CCCallFunc actionWithTarget:pTarget selector:pCallBack];
CCSequence *seq = [CCSequence actions:animate, callback , nil];
NSMutableDictionary *animations;
[animations setValue:seq forKey:pName];
- (void)dealloc {
for (NSString* key in [animations allKeys]) {
CCAction *action = [animations objectForKey:key];
[self stopAction:action];
[action stop];
}
[animations removeAllObjects];
//[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
}
The CCAnimation is not release...
精彩评论