Page 1 of 1

Win 2012 Attempted to read or write protected memory

Posted: Wed Oct 02, 2013 7:50 am
by igor_p
Hello,

We are using your PDF-XChange PRO SDK in our ASP.NET Web Application. Everything works correctly on Windows Server 2008 and 2008 R2 x64. However we have a problem with the Windows Server 2012 x64. Everytime when we try to load a .tif image using a method PXC_AddImageExW(Int32 pdf, String filename, Int32[] image, Int32 pages) from your pxclib40.dll we get following error:

Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Type: System.AccessViolationException
StackTrace: PXC.PXC_AddImageExW(Int32 pdf, String filename, Int32[] image, Int32 pages)

There is a sample of code:

hresult = PXC.PXC_NewDocument( out pdf, licenceKey, developerCode );
if ( IS_DS_FAILED( hresult ) )
{
// creation new pdf document failed
throw new PDFXChangeException( hresult, typeof( PXC ), "Creation a new PDF object" );
}

hresult = PXC.PXC_AddImageExW( pdf, tiffPath, null, 0 );
if ( IS_DS_FAILED( hresult ) )
{
// loading tiff file failed
throw new PDFXChangeException( hresult, typeof( PXC ), "Loading TIFF file" );
}

int pagesCount = hresult;

images = new int[ pagesCount ];

hresult = PXC.PXC_AddImageExW( pdf, tiffPath, images, pagesCount );
if ( IS_DS_FAILED( hresult ) )
{
throw new PDFXChangeException( hresult, typeof( PXC ), "Loading TIFF pages" );
}


Problem appears after the second call of the PXC_AddImageExW() method.

Please for help.

This post is doubled in PDF-X OCR SDK section however it fitts better here. There is the link:
https://forum.pdf-xchange.com/ ... 42&t=18932

Thanks in advance and regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Wed Oct 02, 2013 11:01 am
by Tracker Supp-Stefan
Hi Igor,

To support TIFF images you need to have the fm40tiff.dll distributed with your application. Can you please make sure that this file is present.
Please take a look at which files you need to redistribute for which feature here:
https://help.pdf-xchange.com/DEV/de ... df_xchange_

Regards,
Stefan

Re: Win 2012 Attempted to read or write protected memory

Posted: Wed Oct 02, 2013 12:08 pm
by igor_p
Hi Stefan,

Yes, of course, we are distributing the fm40tiff.dll file. There is a complete list of the dll files that we are distributing in application:
dscrt40.dll
fm40tiff.dll
ixclib40.dll
ocrtools.dll
pxclib40.dll
xccdx40.dll
xcpro40.dll

All files are 64-bit.

Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Wed Oct 02, 2013 7:25 pm
by Ivan - Tracker Software
Does it happen on any TIFF file or only on particular file?
Just in case, can you send us sample TIFF where the problem is reproducible?

Re: Win 2012 Attempted to read or write protected memory

Posted: Thu Oct 03, 2013 10:32 am
by igor_p
Hello,

Our testers use different *.tif or *.tiff files and error occurs every time, so I think that it happens on any TIFF file. There is a sample file:
tifonepage_1169x826_100.zip
(731.49 KiB) Downloaded 279 times
Yesterday, I decided to reproduce it once again and I've noticed that the same error was thrown by the another method - PXC_CloseImage(Int32 pdf, Int32 image). Error details:
Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Type: System.AccessViolationException
StackTrace: at PXC.PXC_CloseImage(Int32 pdf, Int32 image)
at OcrUtility.AddTiffToPdf(String licenceKey, String developerCode, String tiffPath, Int32& pdf)
It's very strange because before it was the PXC_AddImageExW(Int32 pdf, String filename, Int32[] image, Int32 pages).

For easier investigation, I am attaching below the whole body of our method:

Code: Select all

		public static int AddTiffToPdf( string licenceKey, string developerCode, string tiffPath, out int pdf )
		{
			int[] images = null;
			int hresult;
			pdf = -1;
			try
			{
				hresult = PXC.PXC_NewDocument( out pdf, licenceKey, developerCode );
				if ( IS_DS_FAILED( hresult ) )
				{
					// creation new pdf document failed
					throw new PDFXChangeException( hresult, typeof( PXC ), "Creation a new PDF object" );
				}

				hresult = PXC.PXC_AddImageExW( pdf, tiffPath, null, 0 );
				if ( IS_DS_FAILED( hresult ) )
				{
					// loading tiff file failed
					throw new PDFXChangeException( hresult, typeof( PXC ), "Loading TIFF file" );
				}

				int pagesCount = hresult;

				images = new int[ pagesCount ];

				hresult = PXC.PXC_AddImageExW( pdf, tiffPath, images, pagesCount );
				if ( IS_DS_FAILED( hresult ) )
				{
					throw new PDFXChangeException( hresult, typeof( PXC ), "Loading TIFF pages" );
				}

				for ( int i = 0; i < images.Length; i++ )
				{
					try
					{
						// get dimensions
						double width, height;
						hresult = PXC.PXC_GetImageDimension( pdf, images[ i ], out width, out height );
						if ( IS_DS_FAILED( hresult ) )
						{
							throw new PDFXChangeException( hresult, typeof( PXC ), "Getting dimensions of image" );
						}

						// add page
						int page;
						hresult = PXC.PXC_AddPage( pdf, width, height, out page );
						if ( IS_DS_FAILED( hresult ) )
						{
							throw new PDFXChangeException( hresult, typeof( PXC ), "Adding page to PDF" );
						}

						// place image with original size
						hresult = PXC.PXC_PlaceImage( page, images[ i ], 0, height, width, height );
						if ( IS_DS_FAILED( hresult ) )
						{
							throw new PDFXChangeException( hresult, typeof( PXC ), "Placing image on page" );
						}
					}
					catch
					{
						continue;
					}
				}

				return pagesCount;
			}
			finally
			{
				if ( images != null && pdf > 0 )
				{
					for ( int i = 0; i < images.Length; i++ )
					{
						hresult = PXC.PXC_CloseImage( pdf, images[ i ] );
						if ( IS_DS_FAILED( hresult ) )
						{
							// closing images failed
							throw new PDFXChangeException( hresult, typeof( PXC ), "Closing image" );
						}
					}
				}
			}
		}
Note that everything works properly on Windows Server 2008 x64. Have you any ideas what is going wrong?

Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Tue Oct 15, 2013 11:43 pm
by Paul - Tracker Supp
Hi Igor,

I have created a Support Request Ticket for this item (internal only) RT#2036: Win 2012 Attempted to read or write protected memory and have assigned a high priority to it. The development team will get to this as soon as they can.

Please bear in mind that right now it is a major focus releasing the next build of the Editor and we will do our best to balance the needs of this against your need to have this issue resolved in a timely manner.

hth

Re: Win 2012 Attempted to read or write protected memory

Posted: Tue Oct 22, 2013 11:57 pm
by Ivan - Tracker Software
Looks like the problem is with declaration used for functions like:

Code: Select all

PXC.PXC_AddImageExW(Int32 pdf, String filename, Int32[] image, Int32 pages)
image should be an array of pointers, not array of Int32. On 32-bit machines it is the same and causes no problem, but on 64-bit OS for sure there will be memory override, etc.

Re: Win 2012 Attempted to read or write protected memory

Posted: Sat Oct 26, 2013 9:55 am
by igor_p
Hi Ivan,

Thanks alot for your advice! It let us to solve all our problems with processing tiff files.

Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Sun Oct 27, 2013 8:09 pm
by John - Tracker Supp
Please to hear all is well Igor :)

Re: Win 2012 Attempted to read or write protected memory

Posted: Tue Oct 29, 2013 11:05 am
by igor_p
Hello,

Unfortunately, during tests we noticed another problem with pxclib40.dll (x64).
When converting multi-page tiff file to PDF, it converts correctly only odd pages of the tiff file. For even pages, only empty pages are created at the end of the PDF file. For example, if the tiff consists of 8 pages, the PDF will consist of first 4 pages with correct images from odd pages and 4 empty pages at the end.

I've investigated this problem very carefully and I've discovered that method PXC_AddImageExW( IntPtr pdf, string filename, IntPtr[] image, int pages ) creates pointers only for half of the all pages.

Please, look into my code:

Code: Select all

		public static int AddTiffToPdf( string licenceKey, string developerCode, string tiffPath, out IntPtr pdf )
		{
			IntPtr[] images = null;
			int hresult;
			pdf = IntPtr.Zero;

			hresult = PXC.PXC_NewDocument( out pdf, licenceKey, developerCode );
			if ( IS_DS_FAILED( hresult ) )
			{
				// creation new pdf document failed
				throw new PDFXChangeException( hresult, typeof( PXC ), "Creation a new PDF object" );
			}

			hresult = PXC.PXC_AddImageExW( pdf, tiffPath, null, 0 );
			if ( IS_DS_FAILED( hresult ) )
			{
				// loading tiff file failed
				throw new PDFXChangeException( hresult, typeof( PXC ), "Loading TIFF file" );
			}

			int pagesCount = hresult;

			images = new IntPtr[ pagesCount ];

			//making handles to the images located in the file
			hresult = PXC.PXC_AddImageExW( pdf, tiffPath, images, pagesCount );
			if ( IS_DS_FAILED( hresult ) )
			{
				throw new PDFXChangeException( hresult, typeof( PXC ), "Loading TIFF pages" );
			}

			for ( int i = 0; i < images.Length; i++ )
			{
				try
				{
					// get dimensions
					double width, height;
					hresult = PXC.PXC_GetImageDimension( pdf, images[ i ], out width, out height );
					if ( IS_DS_FAILED( hresult ) )
					{
						throw new PDFXChangeException( hresult, typeof( PXC ), "Getting dimensions of image" );
					}

					// add page
					IntPtr page;
					hresult = PXC.PXC_AddPage( pdf, width, height, out page );
					if ( IS_DS_FAILED( hresult ) )
					{
						throw new PDFXChangeException( hresult, typeof( PXC ), "Adding page to PDF" );
					}

					// place image with original size
					// this method automaticaly closes the image object
					hresult = PXC.PXC_PlaceImage( page, images[ i ], 0, height, width, height );
					if ( IS_DS_FAILED( hresult ) )
					{
						throw new PDFXChangeException( hresult, typeof( PXC ), "Placing image on page" );
					}
				}
				catch
				{
					continue;
				}
			}

			return pagesCount;
		}
For example, if the tiff file consists of 8 pages, the images array will look like:
images[0] = 8589934593
images[1] = 17179869187
images[2] = 25769803781
images[3] = 34359738375
images[4] = 0
images[5] = 0
images[6] = 0
images[7] = 0
I've confirmed this issue on Windows Server 2012 x64 and Windows Server 2008 x64.

Below, I'm attaching two files compressed. First is a tiff file before conversion. Second is a PDF after conversion.
files.zip
(258.1 KiB) Downloaded 282 times
When I'm using a x86 version of the dll, everything works great. Unfortunately our solution requires usage of the x64 dlls.
Please for help.

Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Wed Oct 30, 2013 1:56 am
by Ivan - Tracker Software
Looks like it is the issue in the pxclib40.dll.
I can send you fixed version of the library for testing.

Re: Win 2012 Attempted to read or write protected memory

Posted: Wed Oct 30, 2013 9:13 am
by igor_p
That sounds great. Please, send it on my email address i.paszewski[at]lgbs.pl .

Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Wed Oct 30, 2013 12:19 pm
by Tracker Supp-Stefan
Thanks Igor,

We will send you the custom build shortly.

Regards,
Stefan

Re: Win 2012 Attempted to read or write protected memory

Posted: Tue Nov 05, 2013 12:22 pm
by igor_p
Hi Stefan,

Still I did not receive anything. When can I expect an email with a new dll file?

Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Tue Nov 05, 2013 12:43 pm
by Tracker Supp-Stefan
Hi Igor,

Ivan told me he will e-mail you directly when we spoke on the 30th of Oct. I will remind him today and we will see to send it to you asap!

Regards,
Stefan

Re: Win 2012 Attempted to read or write protected memory

Posted: Wed Nov 06, 2013 9:12 am
by Tracker Supp-Stefan
Hi Igor,

I've just e-mailed you the custom .dll Please test with it and let us know the result!

Regards,
Stefan

Re: Win 2012 Attempted to read or write protected memory

Posted: Fri Nov 08, 2013 10:24 am
by igor_p
Hi,

A new pxclib40.dll works perfectly! :) All problems with images conversion disappeared. Thank you very much for your help and commitment. Cooperation with you is a pleasure!

Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Fri Nov 08, 2013 12:35 pm
by Tracker Supp-Stefan
Great to hear that Igor!

I will let Ivan know that the changes he did worked correctly so that we can include them in the next official build as well.

Cheers,
Stefan

Re: Win 2012 Attempted to read or write protected memory

Posted: Thu Nov 21, 2013 9:41 pm
by igor_p
Hello,

During test we noticed, when we add an uncompressed TIFF, the resulting PDF is compressed. When TIFF file is compressed, the resulting PDF has changed compression. We are using exactly the same code as above to convert the TIFF files to PDF. As you can see, we don't use any compression instructions, so we suspect that it's somehow done inside methods from your components. Could you explain us, which methods are responsible for this compression and what compression is it? What are the rules for how the resulting file is rendered? Is there possibility to disable or change that compression? This may be not a bad thing, but we have to know the whole conversion process in case of curious customers :)

I'm attaching two files. First is the TIFF with no compression, second is the PDF after conversion.
20131121-0947_tiff_none_none.zip
(1.5 MiB) Downloaded 269 times
Regards,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Mon Nov 25, 2013 6:12 pm
by Ivan - Tracker Software
PDF files does not use image in their original representation. Instead it supports some encoding methods that may be used to compress raw images data.
Such encodings are: ASCIIHex, ASCII85, LZW, Flate, RunLength, CCITT, JBIG2, DCT (JPEG), and JPX (JPEG 2000).
So, when an image is added to the PDF it is decoded to raw format and than encoded by one (or more) of these encoders.
Which encoder(s) should be used you can specify via PXP_SetCompression function when you create PDF file using pdfxclib.

Re: Win 2012 Attempted to read or write protected memory

Posted: Tue Nov 26, 2013 9:49 am
by igor_p
Thanks for your reply. It's very helpful. Currently, we don't use the PXC_SetCompression method. Could you give me an information, which encoder(s) are used by default when adding images to the PDF?

Best,
Igor

Re: Win 2012 Attempted to read or write protected memory

Posted: Thu Nov 28, 2013 1:56 am
by John - Tracker Supp
The encoders used will be dependant on the image and if you wish to be sure which are used you should use the function as described by Ivan - we cannot advise which options are used without being aware of each and every image specifically added in every circumstance and this is obviously impractical.