0

My task is stuck and it has taken up all the available RAM by the code below after being called several times and running the requested tasks without any problems.

            Document document = new Document();

            using (FileStream newFileStream = new FileStream(tempFile, FileMode.Create, FileAccess.ReadWrite))
            {
                PdfCopy writer = new PdfCopy(document, newFileStream);

                document.Open();

                foreach (var page in pages)
                {

                    PdfReader reader = new PdfReader(Path.Combine(pdfInputPath, page.PdfFileName));

                    reader.ConsolidateNamedDestinations();

                    PdfImportedPage importedPage = writer.GetImportedPage(reader, page.Page);

                    writer.AddPage(importedPage);

                    reader.Close();
                }
                writer.Close();

                document.Close();
            }

what can cause an infinite loop in this piece of code?

and this piece of code also caused an infinite loop

            var reader = new PdfReader(pdfFile);

            using (var outputPdfStream = new FileStream(finalPDFPath, FileMode.Create, FileAccess.Write, FileShare.None))
            {

                var stamper = new PdfStamper(reader, outputPdfStream);
               
                int i = 1;

                foreach (var page in pages)
                {
                    var pdfContentByte = stamper.GetOverContent(i++);

                    string value = GetValue();

                    if (!string.IsNullOrEmpty(value))
                    {
                        var img = GetPage(Information, page, layout, pdfContentByte, value, X_DPI, Y_DPI);
                        //var img = ImgRender(Information, page);
                        pdfContentByte.AddImage(img);
                        pdfContentByte.ClosePath();
                    }
                   
                }
                
                logger.Info("Number of pages after generation  :" + reader.NumberOfPages);
                stamper.Close();
                reader.Close();
            }
Anisse
  • 3
  • 2
  • Can you please edit your question so to add a tag defining which language do you use ? I am afraid you will not get any help without it because people tends to looks specific filters. Besides this, if it works in sequential, I think it could be a race condition. Consider checking all functions/classes are thread-safe. – Jérôme Richard Jul 29 '22 at 21:15
  • Also consider creating a thread dump (stack trace of all current threads, see e.g. [here](https://stackoverflow.com/q/190236/1729265)) as soon as the code is stuck – mkl Aug 02 '22 at 10:25

0 Answers0