Crystal Reports objects disposal

Shabbir Hussain 21 Reputation points
2021-07-08T07:22:56.207+00:00

Hi there,

We are facing issue while disposing crystal reports objects. Currently we are disposing crystal reports objects on page unload which deletes respective crystal report files from windows temp folder but at the same time we have noticed that on average temp files of 15 reports per day keeps on incrementing in temp folder which means that due to some reason report objects are not getting disposed properly. This leads to "Load report failed" error after a few days.

One reason could be the network failure due to which object disposal call back can't do its job.

Question is that is there any way we could dispose old crystal report objects from server through a script a batch or some IIS configuration to avoid "Load report failed" error for which we have to reset our IIS.

Thanks in advance,

Developer technologies ASP.NET ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-07-08T13:48:53.177+00:00

    To be clear CR objects are in memory so when the process terminates the memory is cleared. If you don't do this explicitly then eventually they will get cleaned up as part of garbage collection but that is nondeterministic.

    I believe cleaning up your resources in Unload is the correct place if you cannot clean them up earlier. But personally I never use this event. If you really want to ensure things get cleaned up then keep the object local to the function you need it in and wrap in a using statement. Then most problems go away. Do you really need the CR object to be a field in your page? In my experience this is generally a bad idea, just "create, use, release".

    However I suspect the issue you're having isn't with the CR objects in memory but the files on disk not getting cleaned up because they are still in use. Most likely your CR objects are trying to clean up the files but that is failing so they are orphaned. I know nothing about CR so I cannot answer to how it works but if you have control over the creation of the files then mark them as temp files. Windows will automatically delete them when the last handle is closed on the file.

    If that isn't an option but you do know the filenames then use the TempFileCollection class to manage the files. Add the files to the collection as they are created. When the collection goes away it auto-deletes any files it contains. This is very useful for temp files and we use it a lot.

    However even in these cases it is possible for files to hang around either because they are still in use or the cleanup never happens such as if IIS forcefully terminates the worker process. Therefore you should always have a background process to clean up the files as well. You can use a simple script that is scheduled to run periodically and clean up files (say those older than an hour). This is most useful if you're creating many different files and want them all cleaned up. Alternatively if you want your app to be self-contained then on app startup create a worker service (or thread) that does the cleanup periodically instead. Either approach works well.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.