title: Delphi collection of objects
body: <p>I have created an easy class called <code>TIstruzione</code>; I need to create 4 objects at runtime and store them in an array so that I can access them when I want.</p>
  
  <pre><code>FList: TObjectList&lt;TIstruzione&gt;;
  </code></pre>
  
  <p>I have created this in the public section of TForm1 because I have seen in Delphi's documentation that this is an apposite generic container for classes. Then I am going to manage his lifetime in this way:</p>
  
  <pre><code>procedure TForm1.FormCreate(Sender: TObject);
  begin
   FList.Create;
   FList.OwnsObjects := true;
  end;
  
  procedure TForm1.FormDestroy(Sender: TObject);
  begin
   FList.Destroy;
  end;
  </code></pre>
  
  <p>The <code>OwnsObject</code> ensures that the objects are owned by the list and when I free the list, the objects inside are freed as well. Am I correct?</p>
  
  <p>If you look at the code here, I am trying to populate the list:</p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
  var a: TIstruzione;
  begin
  
   a := TIstruzione.Create;
   try
    a.tipo := Add;
    a.rdest := 2;
    a.dato1 := 7;
    a.dato2 := 5;
    FList.Add(a);
   finally
    a.Free;
   end;
  
    a := TIstruzione.Create;
   try
    a.tipo := Load;
    a.rdest := 1;
    a.dato1 := 2;
    a.dato2 := -1;
    FList.Add(a);
   finally
    a.Free;
   end;
  
  end;
  </code></pre>
  
  <p>I have an error when the program starts. It is an access violation; any idea? Just to be complete, the class is simply this:</p>
  
  <pre><code>type
  
   TTipo = (RISC_Add, RISC_AddI, RISC_Sub, RISC_SubI, RISC_Load, RISC_Store);
   TPip = (MIPS_pipeline, PPC_pipeline);
  
  type
   TIstruzione = class
    public
     tipo: TTipo;
     rdest: integer;
     dato1: integer;
     dato2: integer;
   end;
  </code></pre>