@autoreleasepool块中的内存泄漏

时间:2012-08-21 10:41:10

标签: ios ios5 memory-leaks autorelease

我使用Xcode 4.4,我开发了带有ARC的iOS 5.1应用程序。我有一个线程(除了main)以下定期调用方法。

- (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&amp;创建字符串。 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块。

我错过了什么吗? 感谢。

// 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;

}

// EDIT

- (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);
}

1 个答案:

答案 0 :(得分:0)

您可以使用分配工具“标记堆”。 This似乎有一些关于如何调试此类内存问题的说明。