1586480 - Exception of type 'System.OutOfMemoryException' was thrown when calling CopyTo method

SAP Knowledge Base Article - Public

1586480 - Exception of type 'System.OutOfMemoryException' was thrown when calling CopyTo method

Symptom

The following error message occurs when execute CopyTo method in .NET application

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

Environment

  • Crystal Reports 2008 V1
  • Visual Studio 2005
  • Visual Studio 2008

Reproducing the Issue

C# .NET application contains CopyTo method and error occurs when printing to printer.

e.g.

private void button1_Click(object sender, EventArgs e)
{

System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PaperSize selectedPaperSize = new System.Drawing.Printing.PaperSize();

private const string SELECTED_PRINTER = "Epson LQ-300 ESC/P 2";

CRReport = new ReportDocument();
CRReport.Load(@"C:\test.rpt");

printerSettings.PrinterName = SELECTED_PRINTER;

foreach (System.Drawing.Printing.PaperSize paperSize in printerSettings.PaperSizes)
{
  if (paperSize.PaperName == "Fanfold 210 x 12 in")   //An Enum paper size is machine dependent (i.e. 280)

  {
     selectedPaperSize = paperSize;
  }
}

System.Drawing.Printing.PageSettings pageSettings = new System.Drawing.Printing.PageSettings();
CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();

PrintOptions printOptions = CRReport.PrintOptions;

printOptions.PrinterName = SELECTED_PRINTER;

printOptions.PaperSize = selectedPaperSize.RawKind;
printOptions.DissociatePageSizeAndPrinterPaperSize = true;

PrintLayout.Scaling = PrintLayoutSettings.PrintScaling.Scale;

CRReport.PrintOptions.CopyTo(printerSettings, pageSettings);          
CRReport.PrintToPrinter(printerSettings,pageSettings,false,PrintLayout);

}

Cause

This issue has been identified and is tracked under ID ADAPT01546534.

Resolution

The issue is scheduled to be fixed in a future Service Pack.

Workaround:

Add parameter 'printerSettings' in System.Drawing.Printing.PageSettings() to describe a specified printer to use.

System.Drawing.Printing.PageSettings pageSettings = new System.Drawing.Printing.PageSettings(printerSettings);

Keywords

custom paper size, user defined paper size, enum, server properties, Printing Preferences, paper source, Default Printer, Print Report, Layout , KBA , BI-DEV-NET-SDK , .NET SDK / COM SDK , Problem

Product

Crystal Reports 2008 V1