2014-11-06 99 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