logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
peterb  
#1 Posted : Thursday, January 21, 2016 1:30:52 PM(UTC)
peterb

Rank: Newbie

Groups: Registered
Joined: 1/20/2016(UTC)
Posts: 7

I'm getting a System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt...
Sometimes -- Patagames.Pdf.Pdfium.FPDF_CreateNewDocument_native()
or -- Patagames.Pdf.Pdfium.FPDF_CloseDocument_native(IntPtr document)

I'm running a parallel.foreach (15 threads), each task gets a unique "infile". The error doesn't occur for each thread, but eventually does throw.


Code:

PdfCommon.Initialize();

using (var sourceDoc = Patagames.Pdf.Net.PdfDocument.Load(infile)
{

    for (var i = 1; i <= sourceDoc.Pages.Count; i++)
    {
        string tmppagepdf = Guid.NewGuid().ToString();

        using (FileStream fs = new FileStream(@"e:\workingfolder\" + tmppagepdf, FileMode.Create))
        {
            using (var doc = Patagames.Pdf.Net.PdfDocument.CreateNew()) // <-- EXCEPTION SOMETIMES
            {
                doc.Pages.ImportPages(sourceDoc, i.ToString(), 0); // <-- EXCEPTION SOMETIMES
                doc.Save(fs, SaveFlags.Incremental);
            }
        }

        //do stuff on the file for this page
        // ...

        File.Delete(@"e:\workingfolder\" + tmppagepdf);

    }
}

PdfCommon.Release();

Edited by user Thursday, January 21, 2016 4:41:52 PM(UTC)  | Reason: Not specified

Paul Rayman  
#2 Posted : Thursday, January 21, 2016 5:37:51 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,109

Thanks: 7 times
Was thanked: 130 time(s) in 127 post(s)
you must call the initialize / release methods once in proccess. Otherwise, you are having the situation when one thread already release the library, but the other thread is still working with the library.

Edited by user Thursday, January 21, 2016 5:49:23 PM(UTC)  | Reason: Not specified

peterb  
#3 Posted : Thursday, January 21, 2016 9:55:01 PM(UTC)
peterb

Rank: Newbie

Groups: Registered
Joined: 1/20/2016(UTC)
Posts: 7

that seems to fix it.
So in an asp.net app, I would call initialize() once in Application_Start?
Paul Rayman  
#4 Posted : Thursday, January 21, 2016 10:56:57 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,109

Thanks: 7 times
Was thanked: 130 time(s) in 127 post(s)
Yes.
And don't call Release().
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.