AccessViolationException During Form Processing (c0000005)
While processing PDF forms, IronPDF can throw an intermittent native AccessViolationException (code c0000005) that does not reproduce reliably, even on the same input file. It shows up most often when you access PdfDocument.Form and during PdfDocument.Flatten and PdfDocument.Merge.
AccessViolationException (0xc0000005)
bad conversion while widening narrow string
The crash comes from the interaction between native form-field handles and managed object lifetime. If the PdfDocument is garbage-collected or disposed while native form operations are still in flight, the native layer accesses memory that has already been freed, which produces the access violation.
The repeated "bad conversion while widening narrow string" warnings, along with warnings about removing form-field annotations during flattening, are a separate and benign symptom. They come from decoding internal AcroForm field strings and metadata that contain Japanese or multibyte characters in a given PDF. They do not indicate a problem with Japanese or Unicode support in general, nor with Japanese UNC file paths.
Solution
1. Keep the Document Alive for the Full Operation
Recommended: hold the PdfDocument in scope for the entire duration of form processing. Wrap it in a Using block so its lifetime is explicit, then call GC.KeepAlive(doc) after the form-field loop, before the document is disposed.
Using doc As IronPdf.PdfDocument = IronPdf.PdfDocument.FromFile(tempPdfPath)
For Each field In doc.Form
' Your form field processing here
Next
GC.KeepAlive(doc)
End Using
Using doc As IronPdf.PdfDocument = IronPdf.PdfDocument.FromFile(tempPdfPath)
For Each field In doc.Form
' Your form field processing here
Next
GC.KeepAlive(doc)
End Using
GC.KeepAlive(doc) stops the runtime from collecting the document while native form operations are still running, closing the window in which the native code can hit freed memory.
2. Remove Manual GC.Collect() Calls
Delete any GC.Collect() calls during PDF and form processing. Forcing collection mid-operation can trigger the exact lifetime problem that causes the crash.
3. Target x64 Only
Set the project platform target explicitly to x64, and make sure Prefer 32-bit is disabled. Running as x64 avoids the constraints of a 32-bit process during native form work.

