0

Xcode 4.4 を使用し、ARC を使用して iOS 5.1 用のアプリを開発しています。以下のメソッドを定期的に呼び出すスレッド(メイン以外)があります。

- (void)updateLabels:(NSTimeInterval)timeSinceLastUpdate
{
        int lastTime = self.time;
        self.scoreScale -= ((self.scoreScale-1)/5);
        self.time -= timeSinceLastUpdate;
        if (self.time <= 0) {
            self.time = 0.0;
        }
        if (lastTime != (int) self.time) {
            self.timeChanged = YES;
        }
    @autoreleasepool {
        NSString *timeString = [NSString stringWithFormat:@"%.2d:%.2d",(int)(self.time/60),((int)self.time%60)]; //leak!!!
        if (self.scoreScale <=1) self.scoreScale = 1;

        GLKBaseEffect *scoreValueEffect = [self.sprites objectForKey:@"score_value"];
        if (self.scoreChanged) {
            scoreValueEffect = [[ILUtils sharedInstance] makeEffectWithString:[NSString stringWithFormat:@"%@",self.score] alignment:UITextAlignmentLeft fontName:@"WetLetters EFN" fontSize:20 withViewSize:self.view.bounds.size withRect:CGRectMake(935, 50, 100, 30)];
        }
        scoreValueEffect.transform.modelviewMatrix = [[ILUtils sharedInstance] setupSpriteModelviewMatrixWithViewRect:CGRectMake(955, 46-(30*self.scoreScale-30)/2, 100*self.scoreScale, 30*self.scoreScale)];
        [self.sprites setObject:scoreValueEffect forKey:@"score_value"];

        if (self.timeChanged) {
            GLKBaseEffect *timeValueEffect = [[ILUtils sharedInstance] makeEffectWithString:timeString alignment:UITextAlignmentLeft fontName:@"WetLetters EFN" fontSize:24 withViewSize:self.view.bounds.size withRect:CGRectMake(955, 75, 100, 30)];
            [self.sprites setObject:timeValueEffect forKey:@"time_value"];
        }
    }
}

Instruments を使用すると、時間の経過とともにメモリ使用量が増加することがわかります。文字列を作成する行をコメントアウトすると、メモリ使用量が安定します。また、alloc および init メソッドを使用して文字列を作成しようとしましたが、役に立ちませんでした。https://stackoverflow.com/questions/ask?title=@autoreleasepool%20not%20workingで同様のスレッドを見つけましたが、NSZombieEnabled オプションを有効/無効にしても違いはありません。

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.htmlで述べたように @autorelease ブロックを使用しているにもかかわらず、これらすべての文字列の割り当てが解除されない理由がわかりません.

私は何か見落としてますか?ありがとう。

//編集 によって呼び出されるものmakeEffectWithString:

- (GLKBaseEffect*)makeEffectWithString:(NSString*)string alignment:(UITextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size withViewSize:(CGSize)viewSize withRect:(CGRect)rect
{
    GLKBaseEffect *effect = [[GLKBaseEffect alloc] init];
    effect.texture2d0.enabled = YES;
    effect.texture2d0.name = [self textureWithString:string dimensions:rect.size alignment:alignment fontName:name fontSize:size].name;
    effect.transform.projectionMatrix = [self setupOrthoProjectionMatrixWithViewSize:viewSize];
    effect.transform.modelviewMatrix = [[ILUtils sharedInstance] setupSpriteModelviewMatrixWithViewRect:rect];
    effect.useConstantColor = YES;
    effect.constantColor = GLKVector4Make(0.7f,0.7f,0.7f,0.7f);

    return effect;
}

- (GLKTextureInfo*)textureWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(UITextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size
{
    UIFont *font = [UIFont fontWithName:name size:size];

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, dimensions.width, dimensions.height, 8, dimensions.width, colorSpace, kCGImageAlphaOnly);
    CGColorSpaceRelease(colorSpace);
    CGContextSetGrayFillColor(context, 1.0, 1.0);

    UIGraphicsPushContext(context);
    [string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
    UIGraphicsPopContext();

    NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft];
    GLKTextureInfo *texture = [GLKTextureLoader textureWithCGImage:CGBitmapContextCreateImage(context) options:options error:nil];

    CGContextRelease(context);

    return texture;

}

//編集

- (void)drawSpriteUsingEffect:(GLKBaseEffect*)effect
{

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 20, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 20, BUFFER_OFFSET(12));

    [effect prepareToDraw];
    glDrawArrays(GL_TRIANGLES, 0, 6);
}
4

1 に答える 1

0

割り当てインストゥルメントを使用して「ヒープをマーク」できます。これには、そのようなメモリの問題をデバッグする方法に関するいくつかの指示があるようです。

于 2012-08-22T23:53:07.130 に答える