I'm looking for the correct way in order to remove a TChart and deallocate all the memory. I am using Delphi2007 with the standard TeeChart 7
I create the TChart programmatically:
var parentform: TForm;
begin
newchart:= TChart.Create(parentform);
newchart.Parent:= parentform;
...
Then, I want to remove only the chart from the form (not closing the form itself), but I get only that the chart becomes blank and stays on the form:
newChart.FreeAllSeries;
FreeAndNil(newChart);
if I use
NewChart.Parent := nil,
I don't see the chart anymore, but I think the TChart object still exists (until the parentform is destroyed). Is there a specific method for doing this? Thank you
The most straightforward way to get rid of a TChart
control, or just about any control, for that matter, is to call Free
on it:
newChart.Free;
You can call FreeAndNil
instead if you wish. That has the same effect of calling Free
, but also sets the variable's value to nil
. That's useful if you later test the variable's value to detect whether you still have access to the control. If you never reference the variable again, then FreeAndNil
doesn't get you much.
The control will automatically free the other things it owns, such as the series you manually freed with FreeAllSeries
. You don't need to free them yourself.
Merely clearing the control's Parent
property does not free the control. You can prove that by re-assigning the Parent
property and watching as the control re-appears on the screen. That wouldn't happen if the control had ceased to exist.
If the control remains visible on the screen after you free it, then you have other problems. Maybe the parent control hasn't repainted itself properly. You might try calling Refresh
on the parent control. You might also have multiple controls visible. After all, the question's code creates two chart controls, so maybe one of them is still visible, and you've mistaken it for the control you destroyed.