Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

AV when closing a report

edited July 2003 in General
I have an application that for some reason one of the reports causes an AV
when closing it when viewing the report for the second time. All of my
reports work fine except for this one. I am really lost here. I have
traced it down to the closing of my custom preview when it is destroying the
objects in it.

All my reports are created like so:

frmCustomerReport := TfrmCustomerReport.Create(Self);
frmCustomerReport.Visible := False;
...
{Set Query parameters for report}
...
PreviewCaption := 'Customer List Print Preview'; // caption for print
preview
frmControllerReport.Report.Print;


now on each form for each report I have the following event

procedure TfrmCustomerReport.ppCustomerListPreviewFormClose(Sender:
TObject);
begin

if (TppReport(Sender).Owner <> nil) then
TForm(TppReport(Sender).Owner).Close;

end;

then on the close of the form the report is on I have the following

procedure TfrmCustomerReport.FormClose(Sender: TObject;
var Action: TCloseAction);
begin

Action := caFree;
frmCustomerReport := nil;

end;

here is where it gets odd. If I comment out the Action := caFree line in
this one report I don't get any errors. but if I have that line in I will
get an error when closing the report for the second time.

I don't completely understand why the print preview is having the problem,
why all the reports work except this one.

One more oddity this particular report in the main detail band only has 2
sub reports. If there is no data in the first sub report and only data for
the second sub report I get no errors. But as soon as there is data for the
first sub report I get errors and then I have to comment out the Action :=
caFree line to get rid of the errors. I am really spinning in circles here
and would appreciate and help or direction I could get.

Thanks in advance,
Rodger

Comments

  • edited July 2003
    Rodger,

    The problem is most likely occuring in your second snipit of code. The
    preview form is created by the report object and freed by it so you do not
    need to close it.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited July 2003
    The second snipit should be closing the form when the preview window gets
    closed. That way I can free up the memory associated with the form.

    Thanks,
    Rodger

  • edited July 2003
    This problem started after I took an existing Master->Detail report and
    converted it from the Query's being linked to having the pipelines linked to
    try an speed up the report.

    Thanks in advance,
    Rodger


  • edited July 2003
    Hi Rodger,

    You do not want to close the form inside the PreviewFormClose event because
    there is still code being executed in the TppProducer that uses the form.
    You need to close the form sometime after you make the call to Report.Print
    and you should be ok.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited July 2003
    If I close the form after calling Report.Print and I do not have the
    ModalPreview set to true the preview pops up then closes right away. and the
    user never can see the report. Maybe I'm missing something.

    Thanks,
    Rodger,

  • edited July 2003
    Just a little more info.

    I get an AV now even after the first time closing it. (just made some
    changes to my SQL. Also I get an AV if I use the built in preview or my
    custom one.

    Rodger


  • edited July 2003
    I managed to create a small copy of this.

    Cheers,
    Rodger,

  • edited July 2003
    Hi Rodger,

    I created a small example that does what I believe you need it to. It
    starts a timer inside the PreviewFormClose event and closes the form once
    the timer interval is up. Hope this helps.

    http://www.digital-metaphors.com/tips/CloseFromPreview.zip

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited July 2003
    Not exactly. I don't want to close the main form.

    What I have is a MDI application where the user clicks on a report type and
    it creates the report form, supplies the Query with the necessary param
    values, then calls print to display my own preview window in a MDI child.
    When the user closes the preview I need to close the Report form and free up
    the memory associated with it that was allocated when the user clicked on
    the type of report and the report form was created. (Not sure if I'm
    clearing this up or not.) In your example the report form is auto created
    so it always has memory allocated for it until you close the application.
    In mine I create that form when you would click the preview button. So if I
    don't free it you would keep creating the form every time you clicked
    Preview.

    How's that,
    Rodger


  • edited July 2003
    Rodger,

    Here is another similar example that creates the second form at runtime and
    frees the memory when the preview is closed as you need it to do. Hope this
    helps.

    http://www.digital-metaphors.com/tips/CloseFromPreview.zip

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited July 2003
    By my initial testing it appears that placing a timer to close the form
    instead of using the event delays the closing enough that it allows
    everything to be freed.

    Thanks,
    Rodger


This discussion has been closed.