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 选项没有区别。

我无法弄清楚为什么所有这些字符串都没有被释放,尽管我使用了 @autorelease 块,如https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html .

我错过了什么?谢谢。

//EDIT 调用的东西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 回答