2

We are creating a PDF Preview using PDFToImage DLL of PQScan.We need to create preview image of the first page of PDF.

Whenever I try to get the image, it throws System.StackOverflowException. Which can't be catched in try-catch block and it causes the crash for the application.

try
{
    using (PQScan.PDFToImage.PDFDocument doc = new PQScan.PDFToImage.PDFDocument())
    {
        doc.LoadPDF(UTFPDFPath);
        doc.DPI = 150;
        var temp = doc.PageCount;
    

        using (Bitmap bmp = doc.ToImage(0)) //System.StackOverflowException here
        {
            bmp.Save(BigPrevName, ImageFormat.Gif);
        }
    }
}
catch (Exception ex)
{
}

- $exception {"Exception of type 'System.StackOverflowException' was thrown."} System.StackOverflowException

Arpit Gupta
  • 1,209
  • 1
  • 22
  • 39
  • 1
    Unfortunately, StackOverflowException is a nasty one. Maybe you can isolate it in another process ? – Pac0 Jul 01 '21 at 05:59
  • Does it occur with a specific/any pdf? Is there any documentation of the library that talks about expecting SOException ? – shahkalpesh Jul 01 '21 at 06:00
  • @Pac0 How can I achieve that? – Arpit Gupta Jul 01 '21 at 06:17
  • We can't do much with the third-party library. Maybe we can check if there is the latest version available which might have fix some issues at their end. I faced similar issues with other third-party libraries and almost wasted a month figuring out the app pool crash in case of session timeout which then resolved by upgrading to the latest version. – Rajan Patekar Jul 01 '21 at 06:50
  • The only way to protect your process is to run the suspicious code in a different process. See duplicates. – Peter Duniho Jul 01 '21 at 08:10

2 Answers2

3

The short answer here is "no"; a stack-overflow scenario cannot be reliably caught / recovered from.

  • make sure you're on the most recent version available (it might be fixed!)
  • see whether the documentation lists this as a known issue with possible workaround
  • if it is a paid product, ask them to fix it as part of your support
  • if it is open source, consider fixing it yourself and contributing the fix

If none of these are possible, then the fallback would be to write a second executable that just does this processing (using either IPC or file-based means to get data to/from the second process) - then spawn that second process in the knowledge that if it explodes, only the child process has been burned.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 2
    This helped. I created another process, transferred the code in that process. And now only child process gets killed. Original remains active. Thanks for the help. – Arpit Gupta Jul 01 '21 at 12:45
-1

As MSDN says using is not designed to catch exceptions because it is actually convenient way to do 'try-finally'. You should change it for something like this:

Bitmap bmp;
try
{
    bmp = doc.ToImage(0);
    bmp.Save(BigPrevName, ImageFormat.Gif);
}
catch (Exception ex)
{
    //process exception
}
finally
{
    if (bmp != null)
      bmp.Dispose();
}
  • 2
    the questions clearly shows a `try`/`catch`; the change in this answer will not help - the problem is that stack overflows *cannot be reliably caught* – Marc Gravell Jul 01 '21 at 07:59
  • Yeah, I missed the that outside `catch` would get the exception if it was possible. Thanks for pointing! – doesntcount Jul 01 '21 at 08:08