FMX: destruction of visible controls in tab at runtime


RAD Studio 10.2, c++, FMX:
I have a function that takes XML from a server and programmatically instantiates controls (typically TSpeedButton instances) on a tab of a TTabControl like this:
void TForm1::SetupUserControlsFromXML(_di_IXMLDocument &XMLDoc)


// Various controls instantiated and displayed.


The ClearAll method below clears all controls from the previous call of the above function. Note that TUserControls class inherits a TList.
void TUserControls::ClearAll(void)
TUserControlType *Ctrl;
TSpeedButton *Button;
TLabel *Label;

while (TList::Count) {
// Delete controls.
Ctrl = reinterpret_cast<TUserControlType *>(Items[0]);
switch (Ctrl->ControlType) {
case ctButton:
Button = reinterpret_cast<TSpeedButton *>(Ctrl->Control);

Button->Parent = NULL;
delete Button;
case ctLabel:
Label = reinterpret_cast<TLabel *>(Ctrl->Control);

Label->Parent = NULL;
delete Label;
// Now delete struct instance.
delete Ctrl;
// Remove pointer from list.


The TUserControlType struct is like this:
typedef struct {
enum TVisibleControlType ControlType;

TControl *Control;
} TUserControlType;

If the device disconnects from the server and then reconnects, it calls the SetupUserControlsFromXML() again, clears the controls and repopulates the tab.
The problem is, when the controls are cleared (destroyed), bad things happen, but ONLY if that tab (and hence the controls) are displayed. I get a class segmentation fault (11). I find the occasional pointer to a design time component is NULL!
This problem doesn’t occur if the tab (and hence controls) are not displayed. Rarely, the call stack shows that the exception happened in the tab controls redraw function.
Any suggestions? It’s probably obvious… I even tried hiding the control first before deleting it but the result is the same.
I’ve been stuck on this for a week now!

Comments are closed.