在 iOS 8.1 下,当使用 CGContextDrawPDFPage 将 PDF 页面渲染到图形上下文时,我遇到了内存泄漏。它不会发生在模拟器中,但每次我这样做(在 iPad Air 上)时,我都会得到数百次 272 字节的 malloc 内存泄漏。如果我注释掉 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);
这是堆栈跟踪(反向):
分配
38.58 MB 36.7% 148743 std::__1::list >::list(std::__1::list > const&)
19.61 MB 18.6% 75610 std::__1::vector CG::Path::Subpath CG::Allocator
19.61 MB 18.6% 75610 void std::__1::vector >::__push_back_slow_path(CG::Path::Subpath&&)
19.61 MB 18.6% 75610 CG::Path::Sequence::move_to_point(CGPoint const&, CGAffineTransform const*)
19.61 MB 18.6% 75610 CGPathMoveToPoint
19.59 MB 18.6% 75506 TTrueTypeQuadOutlineContext::AddPoint(bool, int, int)
19.59 MB 18.6% 75506 TTrueTypeFontHandler::RenderGlyph(unsigned short, TTrueTypeQuadOutlineContext&, unsigned int) const
19.59 MB 18.6% 75506 TTrueTypeFontHandler::GetOutlinePath(unsigned short, TGlyphOutlineBatch const&) const
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 const**)
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_glyphs
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
最佳答案
是的,您需要调用 UIGraphicsEndImageContext()
来匹配对 UIGraphicsBeginImageContext()
的每次调用。
根据 UIKit framework reference :
When you are done modifying the context, you must call the
UIGraphicsEndImageContext
function to clean up the bitmap drawing environment and remove the graphics context from the top of the context stack. You should not use theUIGraphicsPopContext
function to remove this type of context from the stack.
关于ios - iOS 8.1 中 CGContextDrawPDFPage 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26783443/