PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 03, 2024 10:40 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Aug 29, 2018 1:45 pm 
Offline

Joined: Wed Aug 29, 2018 1:41 pm
Posts: 1
Version 1.50.4845-RC2a

When running in multiple threads I get this repeatedly.

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at PdfSharp.Fonts.OpenType.OpenTypeFontface.CreateFontSubSet(Dictionary`2 glyphs, Boolean cidFont)
at PdfSharp.Pdf.Advanced.PdfCIDFont.PrepareForSave()
at PdfSharp.Pdf.Advanced.PdfType0Font.PrepareForSave()
at PdfSharp.Pdf.Advanced.PdfFontTable.PrepareForSave()
at PdfSharp.Pdf.PdfDocument.PrepareForSave()
at PdfSharp.Pdf.PdfDocument.DoSave(PdfWriter writer)
at PdfSharp.Pdf.PdfDocument.Save(Stream stream, Boolean closeStream)


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 5:29 am 
Offline

Joined: Tue Sep 06, 2022 5:28 am
Posts: 1
This exception means that you're trying to access a collection item by index, using an invalid index. An index is invalid when it's lower than the collection's lower bound or greater than or equal to the number of elements it contains. Indexing an empty list will always throw an exception. Use a method like Add to append the item to the end of the list, or Insert to place the item in the middle of the list somewhere, etc. You cannot index into a c# list if that offset doesn't exist. IndexOutOfRangeException exception is thrown as a result of developer error. Instead of handling the exception, you should diagnose the cause of the error and correct your code.

Handling the Exception:

Use for-each loop: This automatically handles indices while accessing the elements of an array.

Use Try-Catch: Consider enclosing your code inside a try-catch statement and manipulate the exception accordingly. As mentioned, C# won’t let you access an invalid index and will definitely throw an IndexOutOfRangeException. However, we should be careful inside the block of the catch statement, because if we don’t handle the exception appropriately, we may conceal it and thus, create a bug in your application.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 11:51 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 986
Location: CCAA
sambutle wrote:
Instead of handling the exception, you should diagnose the cause of the error and correct your code.
IIRC this should no longer be an issue with later versions of PDFsharp (1.50.5147 or 1.51.5185-beta).

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group