Receiving NSMallocException on Real devices while using DrawPDFPage

Hi

We're using CGPDFDocument to retrieve the PDF document and draw it in the UIView using CGContext.DrawPDFPage. While doing this operation, I was able to get an NSMallocException with a specific document.

I was able to get this exception only on real devices; emulators are working fine. The device used is the iPad (5th generation), which has iOS 16.7.8.

Steps to replicate:

  1. Open the application at the link (we have attached the sample with PDF file with the issue in the link) https://www.dropbox.com/scl/fi/mosvcnqc0nvxwdjf8k0m5/SimplePDFVieweriOS.zip?rlkey=0ntj75yjg71kjwtwwe06uax5v&st=mmv08e2f&dl=0

  2. Navigate through the pages using the next button.

  3. After the page navigation to page 7, and the application will throw an NSMallocException with the reason: Failed to grow buffer

Platform: Xamarin.iOS

Development tool: Visual Studio 2022

Stack trace for the issue:

Native stack trace:
	0   CoreFoundation                      0x00000001b32e9418 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 37912
	1   libobjc.A.dylib                     0x00000001ac63dc28 objc_exception_throw + 56
	2   CoreFoundation                      0x00000001b347c5c8 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 1689032
	3   CoreFoundation                      0x00000001b3474504 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 1656068
	4   CoreFoundation                      0x00000001b3363160 __CFSafelyReallocate + 68
	5   CoreFoundation                      0x00000001b33aae9c 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 831132
	6   CoreFoundation                      0x00000001b335af2c CFDataSetLength + 152
	7   CoreGraphics                        0x00000001b4ca71a4 CGDataProviderCopyData + 240
	8   ImageIO                             0x00000001b7fafd38 7E9A543E-EE3C-34B7-9EFE-D5F6357F2FA3 + 32056
	9   ImageIO                             0x00000001b7fb1124 7E9A543E-EE3C-34B7-9EFE-D5F6357F2FA3 + 37156
	10  ImageIO                             0x00000001b7ff7124 7E9A543E-EE3C-34B7-9EFE-D5F6357F2FA3 + 323876
	11  ImageIO                             0x00000001b7fb0fe0 CGImageSourceCreateWithDataProvider + 176
	12  CoreGraphics                        0x00000001b4d7ac0c EDFE1CE3-C3C4-30A2-AC92-099D20347781 + 1018892
	13  CoreGraphics                        0x00000001b4d7a9a0 CGPDFImageCreateImage + 208
	14  CoreGraphics                        0x00000001b4decbc8 EDFE1CE3-C3C4-30A2-AC92-099D20347781 + 1485768
	15  CoreGraphics                        0x00000001b4d26e34 EDFE1CE3-C3C4-30A2-AC92-099D20347781 + 675380
	16  CoreGraphics                        0x00000001b4d2f7c4 EDFE1CE3-C3C4-30A2-AC92-099D20347781 + 710596
	17  CoreGraphics                        0x00000001b4ce16dc CGPDFScannerScan + 436
	18  CoreGraphics                        0x00000001b5073198 CGPDFDrawingContextDraw + 112
	19  CoreGraphics                        0x00000001b4d271b0 EDFE1CE3-C3C4-30A2-AC92-099D20347781 + 676272
	20  CoreGraphics                        0x00000001b4d2f7c4 EDFE1CE3-C3C4-30A2-AC92-099D20347781 + 710596
	21  CoreGraphics                        0x00000001b4ce16dc CGPDFScannerScan + 436
	22  CoreGraphics                        0x00000001b4d3f68c CGContextDrawPDFPageWithDrawingCallbacks + 2680
	23  CoreGraphics                        0x00000001b4cdb04c CGContextDrawPDFPage + 32
	24  SimplePDFViewer                     0x0000000100f0cb90 SimplePDFViewer + 1969040
	25  SimplePDFViewer                     0x0000000100eeb3c0 SimplePDFViewer + 1831872
	26  SimplePDFViewer                     0x0000000100d35b14 SimplePDFViewer + 39700
	27  SimplePDFViewer                     0x0000000100e8eb80 SimplePDFViewer + 1452928
	28  SimplePDFViewer                     0x0000000100f483f4 mono_pmip + 25092
	29  SimplePDFViewer                     0x0000000100fd8b90 mono_pmip + 616864
	30  SimplePDFViewer                     0x0000000100fdc014 mono_pmip + 630308
	31  SimplePDFViewer                     0x0000000100d32450 SimplePDFViewer + 25680
	32  SimplePDFViewer                     0x0000000100d32328 SimplePDFViewer + 25384
	33  UIKitCore                           0x00000001b5412150 27A9C298-B702-3C39-8C06-07196E4CD16B + 1667408
	34  QuartzCore                          0x00000001b47990e8 9E40B2EB-260C-3AF1-AE45-A1A551A8C1B9 + 119016
	35  QuartzCore                          0x00000001b480adb8 9E40B2EB-260C-3AF1-AE45-A1A551A8C1B9 + 585144
	36  QuartzCore                          0x00000001b47987a8 9E40B2EB-260C-3AF1-AE45-A1A551A8C1B9 + 116648
	37  QuartzCore                          0x00000001b4798034 9E40B2EB-260C-3AF1-AE45-A1A551A8C1B9 + 114740
	38  QuartzCore                          0x00000001b47a89f0 9E40B2EB-260C-3AF1-AE45-A1A551A8C1B9 + 182768
	39  QuartzCore                          0x00000001b47d5dc0 9E40B2EB-260C-3AF1-AE45-A1A551A8C1B9 + 368064
	40  QuartzCore                          0x00000001b47c0dc8 9E40B2EB-260C-3AF1-AE45-A1A551A8C1B9 + 282056
	41  CoreFoundation                      0x00000001b33698e8 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 563432
	42  CoreFoundation                      0x00000001b32f951c 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 103708
	43  CoreFoundation                      0x00000001b3355214 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 479764
	44  CoreFoundation                      0x00000001b3359d20 CFRunLoopRunSpecific + 584
	45  GraphicsServices                    0x00000001eab69998 GSEventRunModal + 160
	46  UIKitCore                           0x00000001b55ec448 27A9C298-B702-3C39-8C06-07196E4CD16B + 3609672
	47  UIKitCore                           0x00000001b55ec0c0 UIApplicationMain + 312
	48  SimplePDFViewer                     0x000000010106b350 xamarin_UIApplicationMain + 60
	49  SimplePDFViewer                     0x0000000100f0de18 SimplePDFViewer + 1973784
	50  SimplePDFViewer                     0x0000000100eecd4c SimplePDFViewer + 1838412
	51  SimplePDFViewer                     0x0000000100eece70 SimplePDFViewer + 1838704
	52  SimplePDFViewer                     0x0000000100d33ad0 SimplePDFViewer + 31440
	53  SimplePDFViewer                     0x0000000100e8eb80 SimplePDFViewer + 1452928
	54  SimplePDFViewer                     0x0000000100f483f4 mono_pmip + 25092
	55  SimplePDFViewer                     0x0000000100fd8b90 mono_pmip + 616864
	56  SimplePDFViewer                     0x0000000100fddbdc mono_pmip + 637420
	57  SimplePDFViewer                     0x0000000100f2d5ec SimplePDFViewer + 2102764
	58  SimplePDFViewer                     0x00000001010802dc xamarin_log + 22464
	59  SimplePDFViewer                     0x0000000100d334bc SimplePDFViewer + 29884
	60  dyld                                0x00000001d0b04344 8A4B89B7-D348-375B-97B1-FC8A84E3E5CE + 82756

Kindly let us know if you need any further details regarding this issue.

Receiving NSMallocException on Real devices while using DrawPDFPage
 
 
Q