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

Replacing and removing / releasing of report objects

edited November 2012 in General
Hello,

in an update routine I'm going through all objects of several reports and
replace certain objects. In detail: I'm creating a datamodule with a
ppReport on it etc. and then go through a pool of reports, load one, do the
updates, save it and then load the next one and so on. Once everything is
done, I'm releasing the datamodule. It had always worked, but this time I'm
getting an AV.

That's what I do after I've created a new object and placed it at the same
place as the old objects:

if assigned(myNewObject.Region) then begin
TppRegion(myNewObject.Region).RemoveObject(myOldObject);
end;

myReport.Bands[CurrentBandCounter].RemoveObject(myOldObject);

It looks as if that's not enough. It works for one report, but once I modify
two or more and release my datamodule, the AV occurs. Probably because that
object is still available on the owner (i.e. datamodule) like all the other
objects too, but it's not valid anymore or something like that. When I
create and release my datamodule within my loop (i.e. report pool),
everything works fine for two and more reports, but maybe there is a better
solution.

Additionally to the code above I've tried this and it seems to work too:

myOldObject.ChangeOwner(nil);
FreeAndNil(myOldObject);

Is that ok to do so or is there a better solution?

Kind regards,
Mark

Comments

  • edited November 2012
    Hi Mark,

    Once you remove a Report component from a region/band, it is not
    actually freed and is still owned by the main form/datamodule. The
    issue is likely being randomly caused by the old report components still
    "hanging" around after new ones are created.

    It is a good rule of thumb to free these components after you remove
    them from the band/region (as you mentioned at the bottom of your
    message) to be sure no conflicts arise.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.