平铺 PDF 视图正在泄漏内存(非 ARC)

Tiled PDF view is leaking memory (non ARC)

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

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

因此,在分析应用程序时,我发现了似乎是问题所在的代码行。之前的开发者甚至留下了评论:"TODO DANNY leak here 100%"...感谢 Danny...

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

UIView 子视图 class 如下所示:

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

任何帮助将不胜感激!

提前致谢, 艾希

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

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

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

将它们从 superview 中移除并将变量设置为 nil 后,两个 dealloc 方法都被触发,应用程序内存使用不会随着您的页面而永久增加!