Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to cleanup the resources used by a Crystal Reports ReportDocument object?

I am working on an application that uses Crystal Reports for the reporting. It opens a given report in a ReportDocument object, does what it needs to do and then closes the report.

using (var report = OpenReport(reportSourceInfo))
{
    // Do stuff with the report
    report.Close();
}

The OpenReport method does some validation of the source file and returns an open ReportDocument object.

Testing has shown that this code does what it's meant to do and appears to have no issues. The problem I'm really after advice on is when I do a code analysis (CA) build of the reporting project, I get the following CA message:

CA2202 : Microsoft.Usage : Object 'report' can be disposed more than once in method 'CrystalReportingProvider.ReportExecute(ReportSourceInformation)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.

Now obviously I can change the code around so I don't get this CA warning, but my question is should I?

Does the Crystal Reports ReportDocument.Close() method do everything to handle resource cleanup properly? The message seems to indicate that the Close method calls the Dispose method, but that just doesn't seem right.

Any advice would be appreciated.

like image 936
Fooksie Avatar asked Feb 07 '12 06:02

Fooksie


People also ask

What is Crystal reporting tool?

Crystal Reports is a popular Windows-based report writer solution that allows a developer to create reports and dashboards from a variety of data sources with a minimum of code to write. Crystal Reports is owned and developed by SAP.

What is the use of crystal report in vb net?

Crystal Report is a Reporting application that can generate reports from various Data Sources . We can Create Reports , Print and Print Preview of reports from Crystal Reports . Crystal Reports are compatible with most popular development environments like VB.NET etc. and SQL Server also .


2 Answers

While there's a great deal of information available on the web relating to the proper use of memory and the corresponding cleanup of used memory when tasks are complete, at MSDN:IDisposable.Dispose or Stackoverflow:Disposing and Setting to null for example. This gives rise to the prevailing coding convention that if you can call Dispose, then do so.

This convention holds true for objects like FileStreams and SqlDataReader (among others) where you have both Close and Dispose methods, and calling Dispose calls the Close.

What I didn't take into account was "The Crystal Factor". Like them or loathe them, they do things...differently. After a LOT more searching online in the second reply to this SAP SDN article, a SAP employee seems to post the code of the Close method. As you can see, after clearing and disposing all the elements that comprise the ReportDocument object, it calls the ReportDocument.Dispose method as well.

Despite all that, and without knowing how the Dispose method is implemented (properly you would presume as the code does work in its present form), you should code to the proper convention and call the Dispose method or declare it in a Using statement. Just suppress the CA warning.

like image 65
Fooksie Avatar answered Oct 14 '22 10:10

Fooksie


Well, according to this, "Close() ... release[s] the memory that is used by the report." That would indicate that Close() calls Dispose(), so it would be redundant to have both a using statement and Close().

like image 26
Eric Lynes Avatar answered Oct 14 '22 10:10

Eric Lynes