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.
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.
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 .
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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With