Bug 4404 - Strange bug with CGPDFStream
Summary: Strange bug with CGPDFStream
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-04-12 07:40 UTC by Dimitris Tavlikos
Modified: 2012-04-12 11:26 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

CGPDFStream stack trace (4.38 KB, text/rtf)
2012-04-12 07:40 UTC, Dimitris Tavlikos

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Dimitris Tavlikos 2012-04-12 07:40:52 UTC
Created attachment 1659 [details]
CGPDFStream stack trace

I have a PDF file with a page whose "Contents" value is an array of content streams and not a single content stream.

So here is how I get them.

CGPDFDocument pdfDoc = CGPDFDocument.FromFile(pdfFile);
CGPDFPage pdfPage = pdfDoc.GetPage(25);

CGPDFArray contentArr = null;
if (pdfPage.Dictionary.GetArray("Contents", out contentArr))
    // Iterate through the array to output each stream.
    // In my implementation, contentArr.Count is 8.
    for (int i = 0; i < contentArr.Count; i++)
        CGPDFStream stream = null;
        if (contentArr.GetStream(i, out stream))

The first stream in the iteration displays ok. When it gets to the second one, the app crashes. The complete stack trace is attached. The crash occurs at the CGPDFStream's "IntPtr CGPDFStreamCopyData(IntPtr handle);" call which is used in the get accessor of the Data property. I suppose for some reason the stream's handle (which is second in the iteration) gets cleared, when it shouldn't.

The strange thing is that if I add a line of code *above* the "Console.WriteLine" line, it all works ok!

Console.WriteLine("Hello!"); // Some useless code.
Console.WriteLine(Encoding.ASCII.GetString(stream.Data.ToArray())); // Works ok!
Comment 1 Dimitris Tavlikos 2012-04-12 07:43:42 UTC
Forgot to mention that this error occurs on the Simulator. Haven't tested on a device yet.
Comment 2 Rolf Bjarne Kvinge [MSFT] 2012-04-12 07:51:11 UTC
Can you attach a test project (with the pdf in question) where we can try this out (you can mark attachments as private so only Xamarin employees can see them)?

A test case is important given that a simple Console.WriteLine "fixes" the problem...
Comment 3 Dimitris Tavlikos 2012-04-12 07:55:35 UTC
Sure, working on it now. What is the max upload limit? The PDF is ~45MB.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2012-04-12 08:07:09 UTC
The max upload limit is 5mb I think, so you won't be able to attach the pdf (maybe use dropbox, or a similar service)?
Comment 6 Rolf Bjarne Kvinge [MSFT] 2012-04-12 10:35:49 UTC
Thanks for the test case, it made it a lot easier to find.

I've now fixed this issue (master: cba93e1).

You can use the following workaround in the meantime:

[DllImport ("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")]
extern static IntPtr CGPDFStreamCopyData (IntPtr handle, out int format);
public NSData GetData (CGPDFStream stream)
   int format;
   IntPtr obj = CGPDFStreamCopyData (stream.Handle, out format);
   var ret = new NSData (obj);
   CFObject.CFRelease (obj);
   return ret;

and then use it like this in your code:

Console.WriteLine("Content stream {0}:\n{1}", i, Encoding.ASCII.GetString(GetData (stream).ToArray()));
Comment 7 Dimitris Tavlikos 2012-04-12 10:45:49 UTC
Rolf, thanks for this solution! 

In fact, this also solves a similar problem I had with my own custom CGPDFContentStream object with p/invokes (CGPDFContentStream is not yet bound in MonoTouch). I was getting an object with handle=1 and could not understand why. I now understand the problem (sort of at least).

Anyway, thank you!
Comment 8 Dimitris Tavlikos 2012-04-12 11:26:14 UTC
By the way, CFObject.CFRelease is internal, so I just added:

[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", CharSet = CharSet.Unicode)]
extern static IntPtr CFRelease (IntPtr obj);

to use it.