Search Persits Software Knowledge Base

 

Article PS080121168

Attempted to read or write protected memory error

Problem Description

When using a Persits component under .NET via an interop assembly, the following error is generated:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Solution

Possibility 1

This error usually means there is a version mismatch between the component itself (such as asppdf.dll) and the .NET interop assembly (such as ASPPDFLib.dll). For example, the server has version 1.5 of AspPDF installed, but the application uses the interop assembly shipped with version 1.6.

The problem can be fixed as follows:

Method 1 (recommended): Download the component in question from its respective web site and reinstall it. The component and interop assembly DLLs included in the installer are guaranteed to match each other.

Method 2: If reinstallation of the component is not possible, you should create a new interop assembly from the currently installed version of component dll using the command-line utility TLBIMP, and then use that assembly in your .NET application by placing it in the /Bin subdirectory. For example:

c:\>TLBIMP c:\path\asppdf.dll

Possibility 2

Due to the way garbage collection works under .NET, object destruction is delayed and a long-running loop creating new objects may cause the process to run out of memory and crash. Forcing garbage collection inside the loop will help avoid the crash.

Consider the following code snippet converting multi-image TIFFs to multi-page PDFs:

void ConvertTiffToPdf( string inputFile, string outputFile )
{
  IPdfManager pdfManager = new PdfManager();
  IPdfDocument pdfDoc = pdfManager.CreateDocument(Missing.Value);
  IPdfPage page;
  IPdfImage image = pdfDoc.OpenImage(inputFile);
  IPdfParam pdfParam = pdfManager.CreateParam("index=1");

  Single width, height;

  while (image != null)
  {
    width = image.Width * 72 / image.ResolutionX;
    height = image.Height * 72 / image.ResolutionY;
    pdfParam["x"].Value = 0.0f;
    pdfParam["y"].Value = 0.0f;
    page = pdfDoc.Pages.Add(width, height);

    page.Canvas.DrawImage(image, pdfParam);
    pdfParam["index"].Value = pdfParam["index"].Value + 1;
    image = pdfDoc.OpenImage(inputFile, pdfParam);
  }

  pdfDoc.Save( outputFile, false ); }

The ConvertTiffToPdf function, if invoked multiple times in a loop, will soon crash because the repeated creation of the PdfImage objects will cause the process to run out of RAM. To fix this, add a call to System.GC.Collect() inside the loop, as follows:

  while (image != null)
  {
    ...

    System.GC.Collect();
  }

Comments

This article applies to AspUpload, AspEmail, AspEncrypt, AspJpeg, AspPDF, AspUser, and AspGrid.

Created: 1/21/2008 4:58:00 PM
Last Modified: 9/15/2014 11:31:25 AM

Copyright © Persits Software, Inc. 1998 - 2018
For technical support, write to support@persits.com.