2014-11-06 84 views
4

在iOS 8.1中,当使用CGContextDrawPDFPage将PDF页面呈现到图形上下文时,出现内存泄漏。在模拟器中不会发生这种情况,但每次我在iPad Air上都会收到数百次272字节malloc内存泄漏。如果我注释掉CGContextDrawPDFPage,泄漏就会消失。iOS 8.1中CGContextDrawPDFPage的内存泄漏?

是否有其他人获得类似的行为?

CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)data); 
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(dataProvider); 


CGPDFPageRef page; 

// Grab the PDF page 
page = CGPDFDocumentGetPage(pdf, pageNo + 1); 

UIGraphicsBeginImageContext(aRect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextTranslateCTM(context, 0, aRect.size.height); 
CGContextScaleCTM(context, 1, -1); 

CGContextDrawPDFPage(context, page); // <- LEAKING?!?!? 

// Would create the new UIImage from the context 
//image = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGPDFDocumentRelease(pdf); 
CGDataProviderRelease(dataProvider); 

这里有一个堆栈跟踪(逆转):

的malloc

38.58 MB 36.7%,148743的std :: __ 1名::列表> ::列表(STD :: __ 1 ::列表>常量&)

19.61 MB 18.6%75610的std :: __ 1 ::矢量CG ::路径::子路径CG ::分配器

19.61 MB 18.6%75610空隙的std :: __ 1 ::矢量> :: __ push_back_slow_path (CG ::路径::子路径& &)

19.61 MB 18.6%75610 CG ::路径::序列:: move_to_point(CGPoint常量&,CGAffineTransform常量*)

19.61 MB 18.6%75610 CGPathMoveToPoint

19.59 MB 18.6%75506 TTrueTypeQuadOutlineContext :: AddPoint(布尔,INT,INT)

19.59 MB 18.6%75506 TTrueTypeFontHandler :: RenderGlyph(无符号短,TTrueTypeQuadOutlineContext &,无符号整型)常量

19.59 MB 18.6%75506 TTrueTypeFontHandler :: GetOutlinePath(无符号短,TGlyphOutlineBatch常量&)常量

19.59 MB 18.6%75506 FPFontCopyGlyphPath

19.59 MB 18.6%75506 CGFontCreateGlyphPath

19.59 MB 18.6% 75506 CGFontCreateGlyphBitmap

19.59 MB 18.6%75506 CGGlyphBuilder :: create_missing_bitmaps(CGGlyphIdentifier const *,unsigned long,CGGlyphBitmap con ST **)

19.59 MB 18.6%75506个render_glyphs

19.59 MB 18.6%75506个draw_glyph_bitmaps

19.59 MB 18.6%75506个ripc_DrawGlyphs

19.59 MB 18.6%75506个draw_glyphs

19.57 MB 18.6%75434 draw_alph

19.55 MB 18.6%75359 simple_draw

19.55 MB 18.6%75359个CGPDFTextLayoutDrawGlyphs

19.55 MB 18。6%75348 op_TJ

19.55 MB 18.6%75348 pdf_scanner_handle_xname

19.55 MB 18.6%75348 CGPDFScannerScan

19.55 MB 18.6%75348 CGPDFDrawingContextDrawPage

19.55 MB 18.6%75348 pdf_page_draw_in_context

19.55 MB 18.6%75348 CGContextDrawPDFPage

+0

您对此问题有ios8标记,并在标题中提及8.1。你能澄清一下吗?该问题是iOS 8.1还是新问题? – 2014-11-07 08:32:49

+0

另外,你是否在autorelease池中运行这个?为了确定它不是延迟释放,可以尝试在这段代码中添加'@autoreleasepool {}'(注意,显然这会释放你最终想要传递的'image',但至少可以将其消除)。 – 2014-11-07 08:36:45

+0

我认为它也发生在8.0中。我试过包装autorelease块没有成功。我刚刚得到了内存泄漏位置的堆栈跟踪。我会更新这个问题。 – 2014-11-07 10:21:50

回答

0

是的,您需要致电UIGraphicsEndImageContext()以匹配UIGraphicsBeginImageContext()的每个呼叫。

UIKit framework reference

当你完成修改的情况下,你必须调用 UIGraphicsEndImageContext功能来清理位图绘制 环境,并从 上下文堆栈的顶部去除图形上下文。您不应该使用UIGraphicsPopContext函数来从中删除此类型的上下文。

+0

它在那里。我已经打电话来消除泄漏。它仍在泄漏! – 2014-11-07 07:38:04

+0

是的,我可以看到它在那里,有一条评论表明它修复了泄漏。我已经在上面添加了一个解释,说明为什么需要它。从您的评论看来,我似乎误解了您在代码中的评论。我会编辑你的问题,以消除误导性的/ /修复泄漏 – 2014-11-07 08:10:19

+0

谢谢。抱歉不清楚。 (现有的泄漏)真的很奇怪。调用函数100次泄漏很多。在profiler中非常明显! – 2014-11-07 08:30:45