CCAnimation и CCAnimate утечка? - PullRequest
       3

CCAnimation и CCAnimate утечка?

1 голос
/ 13 июля 2011

В моем классе @Monster есть 3 различных анимации ходьбы.

"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];
}

Когда я запускаю это приложение с инструментом производительности - Leaks.Инструментарий отображает оператор "CCAnimation * walk ...", "self.fWalk ...", "walk = ...", "self.bWalk ...", "walk = ...", "self.hWalk .. "вызвать утечки памяти.

Я проверил исходный код CCAnimation и CCAnimate, все они" autorelease ". Я не знаю, почему произошла эта утечка.Есть идеи, что делать?

Ответы [ 2 ]

2 голосов
/ 16 сентября 2011

Вы должны сохранить действия, если планируете использовать их позже, поскольку они автоматически высвобождаются при создании с помощью метода actionWithAction.Например:

self.fWalk = [[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]] retain];

В противном случае ваш класс монстров не владеет действиями, и они автоматически освобождаются при выходе из метода init.

1 голос
/ 22 января 2013

Хорошо, но когда вы сохраняете все анимации в массив, как это:

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];
}

CCAnimation не выпускается ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...