MonoTouch CoreGraphics PDF memory issues with CGPDFDocument and CGPDFPage
我已经与MonoTouch合作了3个星期,在我不得不在应用程序中显示PDF之前,一切都进展顺利。
使用Apple的Quartz 2D编程指南我设法显示了PDF。
问题是该应用程序内存不足。我尝试在CGPDFDocument和CGPDFPage对象上使用Dispose()方法,但随后出现此错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Stacktrace: at (wrapper managed-to-native) MonoTouch.CoreGraphics.CGPDFPage.CGPDFPageRelease (intptr) <0xffffffff> at MonoTouch.CoreGraphics.CGPDFPage.Dispose (bool) <0x00044> at MonoTouch.CoreGraphics.CGPDFPage.Finalize () <0x0002b> at (wrapper runtime-invoke) object.runtime_invoke_virtual_void__this__ (object,intptr,intptr,intptr) <0x0007b> Native stacktrace: 0 FlapMag1 0x00037514 mono_handle_native_sigsegv + 412 1 FlapMag1 0x0000c010 mono_sigsegv_signal_handler + 348 2 libSystem.B.dylib 0x339927f3 _sigtramp + 34 3 libCGVolute.A.dylib 0x31c83d88 CPModelRelease + 24 4 libCGVolute.A.dylib 0x31c84ad4 model_release + 56 5 CoreGraphics 0x3113ced8 pdf_page_finalize + 68 6 CoreFoundation 0x3388fae9 _CFRelease + 168 7 CoreFoundation 0x3388f9c7 CFRelease + 66 8 CoreGraphics 0x3113ce90 CGPDFPageRelease + 20 9 FlapMag1 0x00248cc0 wrapper_managed_to_native_MonoTouch_CoreGraphics_CGPDFPage_CGPDFPageRelease_intptr + 64 * Assertion: should not be reached at ../../../../mono/mini/mini-darwin.c:258 |
这慢慢让我发疯了,因为我尝试了所有我能想到的。
在Apple的示例中,有CGPDFDocumentRelease和CGPDFPageRelease,但是MonoTouch中没有这些。因此,我以为MT会自动管理这些对象,但是显然它不是,或者是越野车。
即使我不对CGPDF对象的Dispose()感到困惑,当我从超级视图中删除包含PDF的视图时,也会发生上述错误。
有人能够在MonoTouch中使用PDF吗?
预先感谢。
更新:
我在Obj-C中使用相同的PDF测试了PDF绘图,发现当我不调用CGPDFDocumentRelease()时,内存消耗具有与MonoTouch相同的快速增长趋势。通过在Obj-C中调用CGPDFDocumentRelease(),内存消耗是正常的。
因此,我认为MonoTouch确实不会释放CGPDFDocument和CGPDFPage对象,并且当我尝试手动或间接(通过删除包含它们的视图)释放它们时,出现了以上错误。
这很浪费时间,现在确实有可能,我必须用Obj-C重写代码... F $#k !!
其他更新:
我仍然无法解释为什么我会遇到与发行版相关的错误,但是我做了一个MonoTouch和一个XCode项目,它们在本质上都做同样的事情:绘制PDF。
我在活动监视器中比较了两者的内存使用情况,发现虽然MonoTouch应用程序不断增加内存使用情况,但XCode却没有。我什至在MonoTouch中的CGPDFDocument对象上调用Dispose(),但是内存消耗仍然增加。
这两个应用程序都不会因与版本相关的错误而崩溃,但确实令人担忧,MonoTouch应用程序中的内存使用量如此之大...
我认为我的问题出在其他地方,但我之所以来到这里,是因为内存消耗过高后主应用程序崩溃了,我似乎找不到降低它的方法,因为我遇到了这些烦人的发布错误。
和其他更新:
在视图的Draw()方法中绘制pdf的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | CGContext context = UIGraphics.GetCurrentContext(); context.SaveState(); CGPDFDocument pdfDoc = CGPDFDocument.FromUrl(_pdfFileUrl); if(pdfDoc.Pages >= 1) { CGPDFPage pdfPage = pdfDoc.GetPage(1); context.ScaleCTM(SCALE.Width, SCALE.Height); // the PDFRectangle is the media box rect of the page, which is hardcoded // for now context.TranslateCTM(-this.PDFRectangle.X, -this.PDFRectangle.Height - this.PDFRectangle.Y); context.DrawPDFPage(pdfPage); } pdfDoc.Dispose(); context.RestoreState(); |
这是由MonoTouch Alpha中的错误引起的,该错误将在1.9.3版中修复。
上面的堆栈跟踪显示PDF发布代码是从终结器线程而不是直接从Dispose调用的。
您愿意为我提供一个显示问题的测试用例吗?有了测试用例后,我们应该能够在几个小时内为您提供修复或解决方法。