Bug 4616 - Null reference in CGContextDrawPDFPage
Summary: Null reference in CGContextDrawPDFPage
Status: RESOLVED DUPLICATE of bug 4678
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-24 07:40 UTC by René Ruppert
Modified: 2012-04-27 17:14 UTC (History)
3 users (show)

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

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:
Status:
RESOLVED DUPLICATE of bug 4678

Description René Ruppert 2012-04-24 07:40:02 UTC
MT 5.2.10, Simulator 5.1

Randomly when trying to draw a PDF page this exception occurs. It is not obvious to me what is NULL here and why it would be. If this is my "fault", please let me know but I thought I'd better report it because it has happened twice so far.

 at (wrapper managed-to-native) MonoTouch.CoreGraphics.CGContext.CGContextDrawPDFPage (intptr,intptr) <IL 0x00024, 0xffffffff>
  at MonoTouch.CoreGraphics.CGContext.DrawPDFPage (MonoTouch.CoreGraphics.CGPDFPage) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/shared/CoreGraphics/CGContext.cs:979
  at iBrainloop.Controllers.PdfViewer.PdfViewerHelpers.GetLowResPagePreviewImage (MonoTouch.CoreGraphics.CGPDFPage,System.Drawing.RectangleF) [0x00108] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/iBrainloop/Controllers/PdfViewer/PdfViewerHelpers.cs:162
  at iBrainloop.Controllers.PdfViewer.PdfViewerHelpers.GetLowResPagePreview (MonoTouch.CoreGraphics.CGPDFPage,System.Drawing.RectangleF) [0x00000] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/iBrainloop/Controllers/PdfViewer/PdfViewerHelpers.cs:111
  at iBrainloop.Controllers.PdfViewer.PdfSinglePageController.RenderHighQualityPdfPage () [0x00060] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/iBrainloop/Controllers/PdfViewer/PdfSinglePageController.cs:663
  at iBrainloop.Controllers.PdfViewer.PdfSinglePageController.ViewWillAppear (bool) [0x0003d] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/iBrainloop/Controllers/PdfViewer/PdfSinglePageController.cs:173
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___sbyte (object,intptr,intptr,intptr) <IL 0x00054, 0xffffffff>
  at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging.void_objc_msgSendSuper_IntPtr_bool (intptr,intptr,intptr,bool) <IL 0x0002e, 0xffffffff>
  at MonoTouch.UIKit.UIViewController.PresentModalViewController (MonoTouch.UIKit.UIViewController,bool) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIViewController.g.cs:261
  at BrainloopMobile.PreviewHelpers/<PreviewLocalDMO>c__AnonStorey1F.<>m__4E (BrainloopMobile.ProgressController/PROGRESS_STATUS) [0x00130] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/BrainloopMobile/Helpers/PreviewHelpers.cs:288
  at BrainloopMobile.ProgressController/<DoWork>c__AnonStorey27.<>m__62 () [0x0002b] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/BrainloopMobile/Controllers/Progress/ProgressController.cs:310
  at MonoTouch.Foundation.NSActionDispatcher.Apply () [0x00000] in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSAction.cs:48
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <IL 0x0004e, 0xffffffff>
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
  at BrainloopMobile.Application.Main (string[]) [0x00000] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/BrainloopMobile/Main.cs:15
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

	0   BrainloopBrowser                    0x00094fbc mono_handle_native_sigsegv + 284
	1   BrainloopBrowser                    0x0000beb8 mono_sigsegv_signal_handler + 248
	2   libsystem_c.dylib                   0x99d5c59b _sigtramp + 43
	3   ???                                 0xffffffff 0x0 + 4294967295
	4   CoreFoundation                      0x960eb468 __CFAllocatorSystemAllocate + 24
	5   CoreFoundation                      0x960eb444 CFAllocatorAllocate + 356
	6   CoreFoundation                      0x960eb1a1 _CFRuntimeCreateInstance + 385
	7   ImageIO                             0x91dc051b _CGImageReadCreate + 83
	8   ImageIO                             0x91dc0444 CGImageReadCreateWithProvider + 284
	9   ImageIO                             0x91dc0291 CGImageSourceCreateWithDataProvider + 220
	10  CoreGraphics                        0x01474ff5 CGImageCreateWithJPEGDataProvider3 + 87
	11  CoreGraphics                        0x01383ce9 create_image_for_image + 229
	12  CoreGraphics                        0x01383bd4 CGPDFImageCreateImage + 191
	13  CoreGraphics                        0x0147135d CGPDFDrawingContextDrawImage + 34
	14  CoreGraphics                        0x01377755 op_Do + 115
	15  CoreGraphics                        0x0146acc9 pdf_scanner_handle_xname + 116
	16  CoreGraphics                        0x0146b194 CGPDFScannerScan + 216
	17  CoreGraphics                        0x013a33c4 CGPDFDrawingContextDrawPage + 506
	18  CoreGraphics                        0x0145e958 pdf_page_draw_in_context + 98
	19  CoreGraphics                        0x01479c67 CGContextDrawPDFPage + 47
	20  ???                                 0x1810e43c 0x0 + 403760188
	21  ???                                 0x1810e3b0 0x0 + 403760048
	22  ???                                 0x1810d054 0x0 + 403755092
	23  ???                                 0x1810c75c 0x0 + 403752796
	24  ???                                 0x1810afbc 0x0 + 403746748
	25  ???                                 0x1810a924 0x0 + 403745060
	26  ???                                 0x15aa69a1 0x0 + 363489697
	27  BrainloopBrowser                    0x00010222 mono_jit_runtime_invoke + 722
	28  BrainloopBrowser                    0x00171f6e mono_runtime_invoke + 126
	29  BrainloopBrowser                    0x00220a62 monotouch_trampoline + 3442
	30  UIKit                               0x0229a38f -[UIViewController _setViewAppearState:isAnimating:] + 158
	31  UIKit                               0x0229a51f __52-[UIViewController _setViewAppearState:isAnimating:]_block_invoke_0 + 171
	32  CoreFoundation                      0x0114c4a5 __NSArrayEnumerate + 597
	33  CoreFoundation                      0x0114c026 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
	34  CoreFoundation                      0x0114bf35 -[NSArray enumerateObjectsUsingBlock:] + 53
	35  UIKit                               0x0229a44d -[UIViewController _setViewAppearState:isAnimating:] + 348
	36  UIKit                               0x0229a51f __52-[UIViewController _setViewAppearState:isAnimating:]_block_invoke_0 + 171
	37  CoreFoundation                      0x0114c4a5 __NSArrayEnumerate + 597
	38  CoreFoundation                      0x0114c026 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
	39  CoreFoundation                      0x0114bf35 -[NSArray enumerateObjectsUsingBlock:] + 53
	40  UIKit                               0x0229a44d -[UIViewController _setViewAppearState:isAnimating:] + 348
	41  UIKit                               0x0229a5eb -[UIViewController __viewWillAppear:] + 62
	42  UIKit                               0x0229a893 -[UIViewController beginAppearanceTransition:animated:] + 203
	43  UIKit                               0x024b89ba -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 5168
	44  UIKit                               0x0229f857 -[UIViewController presentViewController:withTransition:completion:] + 3579
	45  UIKit                               0x0229f9bc -[UIViewController presentViewController:animated:completion:] + 112
	46  UIKit                               0x0229f9fc -[UIViewController presentModalViewController:animated:] + 56
	47  ???                                 0x133c3771 0x0 + 322713457
	48  ???                                 0x133c3678 0x0 + 322713208
	49  ???                                 0x17c0258b 0x0 + 398468491
	50  ???                                 0x1453256a 0x0 + 340993386
	51  ???                                 0x1452ab68 0x0 + 340962152
	52  ???                                 0x09fdea50 0x0 + 167635536
	53  BrainloopBrowser                    0x00010222 mono_jit_runtime_invoke + 722
	54  BrainloopBrowser                    0x00171f6e mono_runtime_invoke + 126
	55  BrainloopBrowser                    0x00220a62 monotouch_trampoline + 3442
	56  CoreFoundation                      0x011eae42 -[NSObject performSelector:withObject:] + 66
	57  Foundation                          0x017649df __NSThreadPerformPerform + 254
	58  CoreFoundation                      0x011bd94f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
	59  CoreFoundation                      0x01120b43 __CFRunLoopDoSources0 + 243
	60  CoreFoundation                      0x01120424 __CFRunLoopRun + 1012
	61  CoreFoundation                      0x0111fd84 CFRunLoopRunSpecific + 212
	62  CoreFoundation                      0x0111fc9b CFRunLoopRunInMode + 123
	63  GraphicsServices                    0x047d87d8 GSEventRunModal + 190
	64  GraphicsServices                    0x047d888a GSEventRun + 103
	65  UIKit                               0x021d1626 UIApplicationMain + 1163
	66  ???                                 0x0ede418d 0x0 + 249446797
	67  ???                                 0x0ede2930 0x0 + 249440560
	68  ???                                 0x0ede2608 0x0 + 249439752
	69  ???                                 0x0ede277e 0x0 + 249440126
	70  BrainloopBrowser                    0x00010222 mono_jit_runtime_invoke + 722
	71  BrainloopBrowser                    0x00171f6e mono_runtime_invoke + 126
	72  BrainloopBrowser                    0x00176294 mono_runtime_exec_main + 420
	73  BrainloopBrowser                    0x00176605 mono_runtime_run_main + 725
	74  BrainloopBrowser                    0x0006b7f5 mono_jit_exec + 149
	75  BrainloopBrowser                    0x0022bccc main + 3052
	76  BrainloopBrowser                    0x00003165 start + 53

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================
Comment 1 René Ruppert 2012-04-24 08:31:50 UTC
For further information, here's my method that is causing the problem:

public static UIImage GetLowResPagePreviewImage (CGPDFPage oPdfPage, RectangleF oTargetRect)
		{
			if(oPdfPage == null)
			{
				return null;
			}
			
			RectangleF oPdfPageRect = PdfViewerHelpers.RotateRectangle( oPdfPage.GetBoxRect (CGPDFBox.Media), oPdfPage.RotationAngle);
			// If preview is requested for the PDF index view, render a smaller version.
			float fAspectScale = 1f;
			if (!oTargetRect.IsEmpty)
			{
				// Resize the PDF page so that it fits the target rectangle. Also scale up if it is smaller than the available area.
				fAspectScale = GetAspectZoomFactor(oTargetRect.Size, oPdfPageRect.Size, true);
				oPdfPageRect = new RectangleF(oPdfPageRect.Location, new SizeF(oPdfPageRect.Size.Width * fAspectScale, oPdfPageRect.Size.Height * fAspectScale));
			}
			
			int iWidth = Convert.ToInt32 ( Math.Ceiling(oPdfPageRect.Size.Width ));
			int iHeight = Convert.ToInt32 ( Math.Ceiling(oPdfPageRect.Size.Height ));
			UIImage oBackgroundImage= null;
			using(CGColorSpace oColorSpace = CGColorSpace.CreateDeviceRGB())
			{
				using(CGBitmapContext oContext = new CGBitmapContext(null, iWidth, iHeight, 8, iWidth * 4, oColorSpace, CGImageAlphaInfo.PremultipliedFirst))
				{
					// First fill the background with white.
					oContext.SetFillColor (1.0f, 1.0f, 1.0f, 1.0f);
					oContext.FillRect (oPdfPageRect);
		    
					// Apply scale. Cannot use GetDrawingTransform() because it does not scale up if the page is smaller than the available area.
					oContext.ConcatCTM( CGAffineTransform.MakeScale(fAspectScale, fAspectScale));
					oContext.InterpolationQuality = CGInterpolationQuality.Low;
					oContext.DrawPDFPage (oPdfPage);
					
					using(CGImage oImage = oContext.ToImage())
					{
						oBackgroundImage = UIImage.FromImage( oImage );
					}
				}
			}
			
			return oBackgroundImage;
		}
Comment 2 Rolf Bjarne Kvinge [MSFT] 2012-04-27 17:14:01 UTC

*** This bug has been marked as a duplicate of bug 4678 ***