1

NSAutoReleasePool の下にいくつかの ccrepeatforever を割り当てています。プールを空にすると、割り当てたメモリが戻ってきません。このコードの何が問題なのか、割り当てたメモリを回復する方法を誰か説明できますか? 関数「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