WinForm with PdfViewCtrl inside crashes hard upon close

Hello everybody,

one more question as I am digging deeper into the PdfViewCtrl functionality using C# / VS 209 and WinForms. With the help of this support forum I have been able to create a WinForm containing a PdfViewCtrl and severa other controls, some of them Buttons, some others are text edit fields. We are passing a PDF file name to this windows and there is a Method we are calling which opens the PDF fgile and attaches the PDF document to the PdfViewCtrl, sets full page display and thumbnails to on.

Then this Form is being displayed by it’s by a form.ShowDialog() call. Everything seems to be fine until the form is being closed. About 2 seconds after the Dialog has been closed, the whole application crashes hard without an exception being raised. The application crash cannot be monitored by Visual Studio 2019 which we are using, it seems to happen in some process not linked to VS which nevertheless must be part of the application.

Any idea what this crash is caused by?

Kind Regards

Axel

Effectively all memory allocated by PDFTron at runtime is owned by a PDFDoc object, though of course a PDFViewCtrl instance also has additional resources. I suspect that you are disposing/closing the PDFDoc instance before PDFViewCtrl is done with it.

The proper way to close is the following.

PDFDoc oldDoc = pdfviewctrl.GetDoc();
pdfviewctrl.CloseDoc(); // or SetDoc(pdfdoc) if you want to open a new doc and reuse the viewer
if(oldDoc != null) oldDoc.Close(); // release all resources and file handles

If the above does not help, then please provide a minimally reproducible project so I can reproduce and review.

Hello Ryan,

sorry, I haven’t been able to resove the issue until now, despite your advice.

Please have a look at the following excerpts from the application’s code

“ViewPdfFile” is a mathod of the WinForm class that contains the PdfViewCtrl control. It’s being called by the parent WinForm to set the filename of the PDF that’s to be displayed. The filename is being passed as the first parameter.

    public void ViewPdfFile (string PDFFileName, string mailPDFFileName)
    {
        pdfFileName = PDFFileName;
        mailPdfFileName = mailPDFFileName;

        doc = new PDFDoc(PDFFileName);
        pdfViewCtrl1.SetDoc(doc);
    }

This successfully opens the PDF file ans makes sure it’s contents are being dsiplayed correctly in the PdfViewCtrl as soon as the Form is being displayed.

The form is being closed by this piece of code:
private void btnOK_Click(object sender, EventArgs e)
{
this.Close();
}

These two code snippets have been from the implementation if the WinForm Class containing the PdfViewCtrl.

The following lines of code are being used to invoke the display of this form:

            pvf.ViewPdfFile(PdfFileName, md.MailPdfFileName);
            if (pvf.ShowDialog() == DialogResult.OK)
            {
                this.splitterSplitY = pvf.splitterSplitY;
            }

pvf is an instance of Class containing the two methods from above. The line this.splitterSplitY … is never reached, the whole application crashes immediately before returning .

Commenting this line out
pdfViewCtrl1.SetDoc(doc);
solves the problem in so far that the apllication doesn’t crash any more - but the PDF file’s contents then isn’t being displayed any more.

Kind Regards

Axel

Where are you calling the code snippet I provided?

If you are not calling that code snippet, then based on what you described, it should be call in the Form’s destructor, so that this.Close() properly disposes the PDF resources after the viewer closes.

If that doesn’t help, then please provide a minimal project I can run to reproduce the issue.

I had inserted your code lines in the FormClosed() handler:

    private void PdfViewerForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        PDFDoc oldDoc = pdfViewCtrl1.GetDoc();
        pdfViewCtrl1.CloseDoc(); // or SetDoc(pdfdoc) if you want to open a new doc and reuse the viewer
        if (oldDoc != null) oldDoc.Close(); // release all resources and file handles
    }

Didn’t change anything.

And as far as I can judge it, these Methods definitely ARE are minimal set of lines of code to recreate the problem.

The whole application still crashes.

This is the full c# code of the Form class. As far as I can see, the doc object doesn’t seem to be destroyed prematurely.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using pdftron;
using pdftron.Common;
using pdftron.SDF;
using pdftron.PDF;

namespace ImportApplicationMail
{
public partial class PdfViewerForm : Form
{
public string pdfFileName = “”;
public string mailPdfFileName = “”;
public PDFDoc doc = null;
public PDFDoc mailDoc = null;
public int splitterSplitY = -1;
//public PDFViewCtrl _pdfview = null;

    public PdfViewerForm()
    {
        InitializeComponent();

    }

    public void ViewPdfFile (string PDFFileName, string mailPDFFileName)
    {
        pdfFileName = PDFFileName;
        mailPdfFileName = mailPDFFileName;

        doc = new PDFDoc(PDFFileName);
        pdfViewCtrl1.SetDoc(doc);

#if otto
mailDoc = new PDFDoc(mailPDFFileName);
pdfViewCtrl1.EnableScrollbar(true);
pdfViewCtrl1.EnableLinkActivation(false);
pdfViewCtrl1.SetSelectedPanel(PDFViewCtrl.PanelType.e_thumbview);
pdfViewCtrl1.SetPageViewMode(PDFViewCtrl.PageViewMode.e_fit_page);
#endif
}

    private void PdfViewerForm_Load(object sender, EventArgs e)
    {

    }

    private void PdfViewerForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        //pdfViewCtrl1.CloseDoc();
        //if (doc != null)
        //    doc.Close();
    }

    // Umschalten zwischen Anzeige des Mailtexts und der Bewerbungsunterlagen
    private void toggleSwitch1_Toggled(object sender, EventArgs e)
    {
        if (switchContents.IsOn)
        {
            // Bewerbungsunterlagen anzeigen
            pdfViewCtrl1.CloseDoc();
            pdfViewCtrl1.SetDoc(doc);
        }
        else
        {
            // Mailinhalt anzeigen
            pdfViewCtrl1.CloseDoc();
            pdfViewCtrl1.SetDoc(mailDoc);
        }

    }

    private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
    {
        splitterSplitY = e.SplitY;
    }

    private void PdfViewerForm_Shown(object sender, EventArgs e)
    {
        this.BringToFront();
        this.WindowState = FormWindowState.Maximized;
        // Gemerkte Position des Splitrahmens wiederherstellen
        if (splitterSplitY != -1)
            splitContainer1.SplitterDistance = splitterSplitY;
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void PdfViewerForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        PDFDoc oldDoc = pdfViewCtrl1.GetDoc();
        pdfViewCtrl1.CloseDoc(); // or SetDoc(pdfdoc) if you want to open a new doc and reuse the viewer
        if (oldDoc != null) oldDoc.Close(); // release all resources and file handles
    }

}

}

Your code appears fine. However, what you sent me was not a minimal project, but code snippets. In particular pdfViewCtrl1 is not declared so unclear the scope/usage of the viewer itself.

To diagnose further I would need one of the following.
a) Our PDFViewSimpleTest project modified to reproduce the issue
b) A minimal project from you that I can run and reproduce.