Attempted to read or write protected memory

I am reading microsoft word byte data from database(multiple records/files 500 or 600 records) and trying to convert to pdf. For testing purpose i have set byteCompleteLetterPdf back to null. I have to later on save it back to database..I am looping through collection and trying to use Parallel.ForEach to use threading to speed up the process. Is Save operation threadsafe?

I am using Aspose.Words v15.1.0.

var file = dbContext.Letters.Where(x=> x.ByteData != null).Select(y=> y);

Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 2 }, file =>
{
try
{
byte[] byteCompleteLetterPdf = null;
byteCompleteLetterPdf = ConvertToPdfWithAspose(file.ByteData);
byteCompleteLetterPdf = null;
}
catch (Exception ex)
{
//DisplayError(ex.TargetSite.Name.ToString() + ": " + ex.ToString().Trim());
}
});


public byte[] ConvertToPdfWithAspose(Byte[] content)
{
MemoryStream documentStream = new MemoryStream();
BinaryWriter binWriter = new BinaryWriter(documentStream);
binWriter.Write(content);

Aspose.Words.Document doc = new Aspose.Words.Document(documentStream);
MemoryStream docxStream = new MemoryStream();
doc.Save(docxStream, Aspose.Words.SaveFormat.Pdf);
BinaryReader br = new BinaryReader(docxStream);
docxStream.Position = 0;

byte[] buffer = new byte[docxStream.Length];
br.Read(buffer, 0, buffer.Length);
return buffer;
}

Can support team take timeout to reply? Its almost 24 hours now…

Hi there,

Thanks for you inquiry. Yes, Aspose.Words does support multi-threading. The only thing you need to make sure is that you
always use separate Document instances per each thread. One thread should use one Document object.

If you are facing any issue while using Aspose.Words, please let us know. We will then provide you more information about your query along with code.

Hi Tahir,

Thanks for your reply. As mentioned, i am using above code to convert to pdf. I get error "Attempted to read or write protected memory. Memory is corrupted".

I get above unhandled exception in ConvertToPdfWithAspose method. Sometimes application closes without even displaying any error. I face the same error when i convert word byte to html and return the string inside Parallel.ForEach loop.

Also note, sometimes it works fine..But most of the time gives error.

Kindly advise.

Thanks.

Hi there,

Thanks for your inquiry. I have tested the scenario with some test documents and have not found the shared issue. Could you please attach your input Word documents here for testing? I will investigate the issue on my side and provide you more information.

Hi Tahir,


Couple of questions.

1. Have you used code i posted above or you made any changes?
2. How many documents have you tried processing? I was processing almost 50-100 documents…
3. Are you reading byte data from database?

You can use any word document. with some content in it. and populate byte data(doc document) in your database. I have table with three columns LetterId, WordCompleteLetter(meaning full word document as byte …I want to convert this to full pdf) and WordContent(only content inside document …i want to convert this to html)…

Please find attached file with code i have used. Please note i am adding all processed documents in collection “ConcurrentBag”.If i removed and dont add in collection also it breaks before only when processing the files…


Note: You should call method “ProcessDocuments()” on button click event. Also include Aspose 15.1.0 as dll. Also process 100-200 documents…try it more than once…

Regards.

Hi there,

Thanks for your inquiry. I used the same code shared in your first post. I tested it with simple documents (around 3000 documents). I read the documents from disk and passing bytes of date to ConvertToPdfWithAspose method.

It would be great if you please share following detail for investigation purposes.


  • Please attach your input Word documents.
  • Please

    create a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate
    your output document

Please compress your documents, you’re getting this problem with, in .rar or .zip format and share it via SkyDrive, DropBox or any free file sharing service. Once we have your input Word
documents, we will investigate it on our side and provide you more
information.