0

因此,我从事一个非常大且非常古老的遗留项目,该项目大约在 5 年前用非 ARC 目标 c 代码编码。我接手了以前的开发人员,现在必须维护这个野兽。

该应用程序允许用户阅读作为单独文件下载的 PDF 页面。问题是在屏幕上显示 PDF 的视图严重泄漏内存。大约 5 分钟的简单分页会导致消耗大约 400 兆的内存。

因此,在分析应用程序时,我发现了似乎是问题的代码行。之前的开发者甚至留下了评论:“TODO DANNY 100% 在这里泄漏”... 谢谢 Danny...

无论如何,我已经尝试了很多事情,我一生都无法理解为什么这是泄漏内存。我的大部分工作和经验都与 ARC 项目有关,所以我很难理解为什么这段代码会泄漏。

UIView 子视图类如下所示:

- (void)dealloc {
    self.pauseDraw = YES;
    CGPDFPageRelease(pdfPage);  
    CGPDFDocumentRelease(pdfDocumentRef);
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame andScale:(CGFloat)scale{
    if ((self = [super initWithFrame:frame])) {        
        CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];

        if ([[[UIDevice currentDevice] modelName] isEqualToString:@"iPad 3G"]) {
            tiledLayer.tileSize = CGSizeMake(1024, 1024);
            tiledLayer.levelsOfDetail = 4;
            tiledLayer.levelsOfDetailBias = 4;
        } else {
            tiledLayer.levelsOfDetail = 4;
            tiledLayer.levelsOfDetailBias = 4;
            tiledLayer.tileSize = CGSizeMake(2048, 2048);
        }
        myScale = scale;
    }
    return self;
}

+ (Class)layerClass {
    return [CATiledLayer class];
}

- (void)setPage:(CGPDFPageRef)newPage andDocument:(CGPDFDocumentRef)docRef {
    CGPDFPageRelease(pdfPage);
    CGPDFDocumentRelease(pdfDocumentRef);
    self->pdfPage = CGPDFPageRetain(newPage);
    self->pdfDocumentRef = CGPDFDocumentRetain(docRef);
}

- (void) releasePDFsFromMemory {
    CGPDFPageRelease(pdfPage);
    CGPDFDocumentRelease(pdfDocumentRef);
}

-(void)drawRect:(CGRect)r {}

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context {

    if(!self.superview){
        return;
    }

    if (pauseDraw) {
        return;
    }   

    @autoreleasepool {
        CGRect cropBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
        int rotate = CGPDFPageGetRotationAngle(pdfPage);

        CGContextSaveGState(context);   
        CGContextTranslateCTM(context, 0, 0);
        CGContextScaleCTM(context, myScale, myScale);

        switch (rotate) {
            case 0:
                CGContextTranslateCTM(context, 0, cropBox.size.height);
                CGContextScaleCTM(context, 1, -1);
                break;
            case 90:
                CGContextScaleCTM(context, 1, -1);
                CGContextRotateCTM(context, -M_PI / 2);
                break;
            case 180:
            case -180:
                CGContextScaleCTM(context, 1, -1);
                CGContextTranslateCTM(context, cropBox.size.width, 0);
                CGContextRotateCTM(context, M_PI);
                break;
            case 270:
            case -90:
                self.frame = CGRectMake(0, 0, 768, 1004);
                CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
                CGContextRotateCTM(context, M_PI / 2);
                CGContextScaleCTM(context, -1, 1);
                break;
        }

        CGRect clipRect = CGRectMake(0, 0, cropBox.size.width, cropBox.size.height);
        CGContextAddRect(context, clipRect);
        CGContextClip(context);

        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(context, clipRect);

        CGContextTranslateCTM(context, -cropBox.origin.x, -cropBox.origin.y);

        CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
        CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

        // TODO DANNY leak here 100% ->
        CGContextDrawPDFPage(context, pdfPage);

        CGContextRestoreGState(context);
    }    
}

任何帮助都感激不尽!

在此先感谢, 伊什

4

1 回答 1

1

所以我设法解决了这个问题。

问题不在于我上面粘贴的代码(为误导而道歉),问题是在父视图的 dealloc 方法中,以前的开发人员没有从超级视图中删除 tiledPDF 视图,并且在调用 release 后没有将变量设置为 nil .

我通过在 dealloc 方法中放置一个 NSLog 发现了这一点,并且看到父视图正在解除分配,但是 2 个平铺的 PDF 子视图根本没有解除分配。

从超级视图中删除它们并将变量设置为 nil 后,两个 dealloc 方法都被触发了,并且应用程序内存使用量不会在您页面时永久增加!

于 2015-05-28T13:02:45.260 回答