moonpdf icon indicating copy to clipboard operation
moonpdf copied to clipboard

app crashes

Open Clemani opened this issue 8 years ago • 0 comments

It happens not often, but sometimes my app crashes on calling fz_run_page with System.AccessViolationException. I cannot re-produce it, it happens occasionally on my client installations.

I guess the crashes are caused by the pinned but immediately un-pinned memory:

            public PdfFileStream(IPdfSource source)
            {
                if (source is FileSource)
                {
                    var fs = (FileSource)source;
                    Context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT); // Creates the context
                    Stream = NativeMethods.OpenFile(Context, fs.Filename); // opens file as a stream
                    Document = NativeMethods.OpenDocumentStream(Context, ".pdf", Stream); // opens the document
                }
                else if (source is MemorySource)
                {
                    var ms = (MemorySource)source;
                    Context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT); // Creates the context
                    GCHandle pinnedArray = GCHandle.Alloc(ms.Bytes, GCHandleType.Pinned);
                    IntPtr pointer = pinnedArray.AddrOfPinnedObject();
                    Stream = NativeMethods.OpenStream(Context, pointer, ms.Bytes.Length); // opens file as a stream
                    Document = NativeMethods.OpenDocumentStream(Context, ".pdf", Stream); // opens the document
                    pinnedArray.Free();
                }
}

I think pinnedArray must be an instance variable, and pinnedArray.Free() called in Dispose(), so the memory cannot get moved around by the garbage collector.

Clemani avatar Jun 30 '17 18:06 Clemani