CGPDFDocument 和 CGPDFPage 的 MonoTouch CoreGraphics PDF 内存问题 [英] MonoTouch CoreGraphics PDF memory issues with CGPDFDocument and CGPDFPage

查看:30
本文介绍了CGPDFDocument 和 CGPDFPage 的 MonoTouch CoreGraphics PDF 内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MonoTouch 已经 3 周了,一切都很顺利,直到我不得不在我的应用中显示 PDF.

I've been working with MonoTouch for 3 weeks now and everything was going great until I had to display PDFs in my app.

使用苹果的 Quartz 2D 编程指南 我设法显示了 PDF.

Using Apple's Quartz 2D Programming Guide I managed to display the PDF.

问题是,应用程序内存不足.我尝试在 CGPDFDocument 和 CGPDFPage 对象上使用 Dispose() 方法,但随后出现此错误:

The problem is, that the app runs out of memory. I tried to use the Dispose() methods on the CGPDFDocument and CGPDFPage objects, but then I get this error:

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

这慢慢让我发疯,因为我尝试了我能想到的一切.

This slowly drives me mad, because I tried everything I could think of.

在 Apple 的示例中,有 CGPDFDocumentRelease 和 CGPDFPageRelease,但 MonoTouch 中缺少这些.因此我认为 MT 会自动管理这些对象,但显然它没有或者它有问题.

In Apple's example there is the CGPDFDocumentRelease and CGPDFPageRelease, but these are missing from MonoTouch. Therefore I thought MT manages these object automatically, but apparently it doesn't or it's buggy.

即使我没有弄乱 CGPDF 对象的 Dispose(),当我从超级视图中删除包含 PDF 的视图时,也会发生上述错误.

Even when I don't mess with the CGPDF object's Dispose(), the above error occurs, when I remove the view, that contains the PDF, from the superview.

有人能够在 MonoTouch 中处理 PDF 吗?

Has someone been able to work with PDFs in MonoTouch?

提前致谢.

更新:
我使用相同的 PDF 在 Obj-C 中测试了 PDF 绘图,发现当我不调用 CGPDFDocumentRelease() 时,内存消耗与 MonoTouch 中的快速增长趋势相同.通过调用Obj-C中的CGPDFDocumentRelease(),内存消耗是正常的.
因此,我认为 MonoTouch 确实没有释放 CGPDFDocument 和 CGPDFPage 对象,当我尝试手动或间接释放它们时(通过删除包含它们的视图),我收到上述错误.

UPDATE:
I tested PDF drawing in Obj-C with the same PDFs and found out that when I don't call CGPDFDocumentRelease() the memory consumption has the same tendency to go rapidly up as in MonoTouch. By calling the CGPDFDocumentRelease() in Obj-C the memory consumption is normal.
Therefore I think that MonoTouch really doesn't free up the CGPDFDocument and CGPDFPage objects and when I try to free them manually or indirectly (by removing the views which contain them), I get the above error.

这很费时间,现在很有可能我必须用 Obj-C 重写代码...... F$#k!!

This sucks big time and it is a real possibility now, that I have to rewrite the code in Obj-C ... F$#k!!

另一个更新:
我仍然无法破解为什么会出现与发布相关的错误,但我制作了一个 MonoTouch 和一个 XCode 项目,两者基本上都做相同的事情:绘制 PDF.
我在 Activity Monitor 中比较了两者的内存使用情况,发现虽然 MonoTouch 应用程序不断增加内存使用量,但 XCode 应用程序没有.我什至在 MonoTouch 中调用 CGPDFDocument 对象上的 Dispose(),但内存消耗仍然增加.
这两个应用程序都没有因发布相关错误而崩溃,但确实令人担忧的是 MonoTouch 应用程序中的内存使用量如此之大...

ANOTHER UPDATE:
I'm still not able to decypher why I get the release related errors, but I made a MonoTouch and an XCode project, where both do essentially the same thing: draw PDFs.
I compared the memory usage of both in Activity Monitor and found out that while the MonoTouch app increases memory usage constantly, the XCode one doesn't. I even call the Dispose() on the CGPDFDocument object in MonoTouch and still the memory consumption increases.
Neither apps crash with the release related error, but it's really worrying that the mem usage is so drasticly larger in the MonoTouch app...

我认为我的问题出在其他地方,但我来到这里是因为在内存消耗过高后我的主应用程序崩溃了,而且我似乎无法找到一种方法来降低它,因为我遇到了这些烦人的发布错误.

I figure that my problem lies somewhere else, but I got here because my main app crashed after mem consumption got too high and I can't seem to find a way to bring it down, because I get these annoying release errors.

和另一个更新:
在视图的 Draw() 方法中绘制 pdf 的代码:

AND ANOTHER UPDATE:
The code that draws the pdf in the view's Draw() method:

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 版中修复

This was caused by a bug in the MonoTouch Alpha which will be fixed in version 1.9.3

这篇关于CGPDFDocument 和 CGPDFPage 的 MonoTouch CoreGraphics PDF 内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆