1

我在 NSAutoReleasePool 下分配了一些 ccrepeatforevers。当我排空池时,我没有取回分配的内存。谁能解释这段代码有什么问题以及如何恢复分配的内存?在函数“initPlayerAnimations”中,变量“a”跟踪内存,如该函数中的注释所示。

-(void) initPlayerAnimations
{
    if (!playerSpriteDirty) return;
    playerSpriteDirty = false;

    double a = [CCDirector getAvailableMegaBytes];
    // a = 32 megabytes approximately

    NSAutoreleasePool *pool;
    pool = [[NSAutoreleasePool alloc] init];

[self wipeAnimations];

    NSString *aString;
    aString = @"YHUMANS";
    [walkAnimations addObject:[self getARepeatForeverFor:aString With:15 AndDelay:mainDelay/15]];
    aString = @"YHUMANE";
    [walkAnimations addObject:[self getARepeatForeverFor:aString With:15 AndDelay:mainDelay/15]];
    aString = @"YHUMANNE";
    [walkAnimations addObject:[self getARepeatForeverFor:aString With:15 AndDelay:mainDelay/15]];
    aString = @"YHUMANN";
    [walkAnimations addObject:[self getARepeatForeverFor:aString With:15 AndDelay:mainDelay/15]];
    aString = @"YHUMANSE";
    [walkAnimations addObject:[self getARepeatForeverFor:aString With:15 AndDelay:mainDelay/15]];

    aString = @"YHUMANSI";
    [idleAnimations addObject:[self getARepeatForeverFor:aString With:25 AndDelay:mainDelay/10]];
    aString = @"YHUMANEI";
    [idleAnimations addObject:[self getARepeatForeverFor:aString With:25 AndDelay:mainDelay/10]];
    aString = @"YHUMANNEI";
    [idleAnimations addObject:[self getARepeatForeverFor:aString With:25 AndDelay:mainDelay/10]];
    aString = @"YHUMANNI";
    [idleAnimations addObject:[self getARepeatForeverFor:aString With:25 AndDelay:mainDelay/10]];
    aString = @"YHUMANSEI";
    [idleAnimations addObject:[self getARepeatForeverFor:aString With:25 AndDelay:mainDelay/10]];

    a = [CCDirector getAvailableMegaBytes];
    // a = 3 megabytes approximately

    [self wipeAnimations];

    [pool drain];

    a = [CCDirector getAvailableMegaBytes];
    // a = 5 megabytes approximately; where is the rest of memory?
}

-(CCAnimation*) animationWithFile2:(NSString*)name
                   frameCount:(int)frameCount
                        delay:(float)delay
{
    NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
    for (int i = 1; i < (1+frameCount); i++)
    {
        NSString* file = [NSString stringWithFormat:@"%@%i.PNG", name, i];
        CCTexture2D* texture = [self createPlayerSpriteFor:file];
        CGSize texSize = texture.contentSize;
        CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
        CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
        [frames addObject:frame];
    }
    return [CCAnimation animationWithFrames:frames delay:delay];
}

-(CCRepeatForever*) getARepeatForeverFor:(NSString*)aString With:(int)num AndDelay:(double)delay
{
    CCAnimation* anim =  [self animationWithFile2:aString
                                            frameCount:num
                                                 delay:delay];
    CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];

    return repeat;
}

-(void) wipeAnimations
{
    [walkAnimations removeAllObjects];
    [idleAnimations removeAllObjects];
}

-(CCTexture2D*) createPlayerSpriteFor:(NSString*)file
{
    CCSprite *aSprite = [[CCSprite alloc]initWithFile:file];
    aSprite.anchorPoint = ccp(0,0);

    NSArray *anArray = [file componentsSeparatedByString:@"."];
    NSString *aString = [anArray objectAtIndex:0];
    CCSprite *bSprite = NULL;

    if (thePlayer.mainHand) {
        aString = [aString stringByAppendingString:@"SWORD.PNG"];
        bSprite = [[CCSprite alloc]initWithFile:aString];
        bSprite.anchorPoint = ccp(0,0);
    }

   CCRenderTexture *rt = [[CCRenderTexture alloc]initWithWidth:aSprite.textureRect.size.width height:aSprite.textureRect.size.height pixelFormat:kCCTexture2DPixelFormat_Default];

   [rt begin];

   [aSprite visit];
   if (bSprite)
       [bSprite visit];

   [rt end];

   CCTexture2D *aTex = rt.sprite.texture;
   [aTex setAntiAliasTexParameters];

   [aSprite release];
   [bSprite release];


   return aTex;
}
4

0 回答 0