title: MonoTouch CoreGraphics PDF memory issues with CGPDFDocument and CGPDFPage
body: <p>I've been working with MonoTouch for 3 weeks now and everything was going great until I had to display PDFs in my app.  </p>
  
  <p>Using Apple's <a href="http://developer.apple.com/mac/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html#//apple_ref/doc/uid/TP30001066-CH214-TPXREF109" rel="nofollow noreferrer">Quartz 2D Programming Guide</a> I managed to display the PDF.  </p>
  
  <p>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:</p>
  
  <pre><code>Stacktrace:
  
    at (wrapper managed-to-native) MonoTouch.CoreGraphics.CGPDFPage.CGPDFPageRelease (intptr) &lt;0xffffffff&gt;
    at MonoTouch.CoreGraphics.CGPDFPage.Dispose (bool) &lt;0x00044&gt;
    at MonoTouch.CoreGraphics.CGPDFPage.Finalize () &lt;0x0002b&gt;
    at (wrapper runtime-invoke) object.runtime_invoke_virtual_void__this__ (object,intptr,intptr,intptr) &lt;0x0007b&gt;
  
  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
  </code></pre>
  
  <p>This slowly drives me mad, because I tried everything I could think of.  </p>
  
  <p>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. </p>
  
  <p>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.  </p>
  
  <p>Has someone been able to work with PDFs in MonoTouch?  </p>
  
  <p>Thanks in advance.</p>
  
  <p>UPDATE:<br>
  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.<br>
  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.  </p>
  
  <p>This sucks big time and it is a real possibility now, that I have to rewrite the code in Obj-C ... F$#k!!</p>
  
  <p>ANOTHER UPDATE:<br>
  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.<br>
  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.<br>
  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...</p>
  
  <p>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.</p>
  
  <p>AND ANOTHER UPDATE:<br>
  The code that draws the pdf in the view's Draw() method:</p>
  
  <pre><code>CGContext context = UIGraphics.GetCurrentContext();
  
  context.SaveState();
  
  CGPDFDocument pdfDoc = CGPDFDocument.FromUrl(_pdfFileUrl);
  if(pdfDoc.Pages &gt;= 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();
  </code></pre>