Remove components at runtime in Delphi with Android

When creating a TButton dynamically at runtime within a TVertScrollBox. When I need to delete this button it does not disappear from the screen. This problem only occurs when running the application on Android (in Windows it works correctly).

To delete the buttons created at runtime inside TVertScrollBox I run this routine:

   for i := vsScroll.ComponentCount-1 downto 0 do
      if vsScroll.Components[i] is TButton then
         TButton(vsScroll.Components[i]).Free;

How do I remove the created components?

OBS.: No use using TButton(vsScroll.Components[i]).Destroy instead of Free.

Author: wBB, 2017-11-15

1 answers

I figured it out. For Android the function of releasing the memory component and consequent deletion of the screen is DisposeOf and not Free, as I used to use in VCL components.

Delphi Help says the following:

DisposeOf forces the execution of the destructor code in an object. The new Delphi mobile compilers introduces a new dispose pattern implemented by calling DisposeOf, which executes the destructor code even if there are variables with pending references to the object. After the DisposeOf method is called, the object is placed in a special state, the Disposed state. This means that the destructor is not called again if DisposeOf is called again, or if the reference count reaches zero (the moment in which the memory is released).

The routine of deleting components from my list created at runtime looked like this:

   for i := vsScroll.ComponentCount-1 downto 0 do
      if vsScroll.Components[i] is TButton then
         TButton(vsScroll.Components[i]).DisposeOf;
 1
Author: wBB, 2017-11-15 18:44:34