title: Freeing buttons in a list in OnClick
body: <p>What I'm trying to achieve, in simplified form, is to create a list of dynamically created buttons. When clicking on one of the buttons it should be removed from the list and its object should be freed. My approach is :</p>
  
  <ul>
  <li>Create a <code>TList&lt;TButton&gt;</code></li>
  <li>Create a couple of <code>TButton</code> objects and add them to the <code>TList&lt;TButton&gt;</code></li>
  <li>Assign the <code>Form</code> as the <code>Parent</code> for each of the created <code>TButton</code> objects</li>
  <li>Assign a <code>Position</code> for each of the created <code>TButton</code> objects</li>
  <li>Assign an <code>OnClick</code> handler method to each of the created <code>TButton</code> objects</li>
  <li>The <code>OnClick</code> handler sets the <code>Sender</code> <code>TButton</code>'s <code>Parent</code> to <code>nil</code> and deletes it from the <code>TList&lt;TButton&gt;</code>, so that ARC can free the <code>TButton</code> object that was clicked on.</li>
  </ul>
  
  <p>When I click on one of the dynamically created buttons, I get a "Segmentation Fault". I suspect it is because I am freeing the <code>TButton</code> object in its own <code>OnClick</code> handler and the class is trying to do some other stuff with it after my handler.</p>
  
  <p>I have tested this on Android. I assume the same will happen on iOS, or any other ARC platform for that matter.</p>
  
  <p>Is there a better/right way to do this, or another approach I should be following to get it working the way I want?</p>
  
  <p>Here is some example code. It is for a form with one design-time button (<code>Button1</code>) on it. Clicking this button repeatedly dynamically creates new buttons and adds them to the list.</p>
  
  <pre><code>unit Unit2;
  
  interface
  
  uses
    System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
    FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
    FMX.Controls.Presentation, FMX.StdCtrls, System.Generics.Collections;
  
  type
    TForm2 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
      procedure FormCreate(Sender: TObject);
    private
      ButtonList : TList&lt;TButton&gt;;
      procedure ButtonClick(Sender: TObject);
      { Private declarations }
    public
      { Public declarations }
    end;
  
  var
    Form2: TForm2;
  
  implementation
  
  {$R *.fmx}
  
  procedure TForm2.ButtonClick(Sender: TObject);
  var
      pos : Integer;
  begin
      pos := ButtonList.IndexOf(TButton(Sender));
      TButton(Sender).Parent := nil;
      ButtonList.Delete(pos);
  end;
  
  procedure TForm2.FormCreate(Sender: TObject);
  begin
      ButtonList := TList&lt;TButton&gt;.Create;
  end;
  
  procedure TForm2.Button1Click(Sender: TObject);
  var
      pos : Integer;
  begin
      pos := ButtonList.Add(TButton.Create(nil));
      ButtonList.Items[pos].Parent := Form2;
      ButtonList.Items[pos].Position.Y := 50 * ButtonList.Count;
      ButtonList.Items[pos].OnClick := ButtonClick;
  end;
  
  end.
  </code></pre>