Q: Trouble with dynamic array of timage and tlabel in delphi I want to add an attachment, and have the form grow longer each time an attachment is added, to make room for a line that holds information about the attachment with a label and some 16X16 images. For this I chose to use a dynamic array (not sure whether that's best). each time an attachment is added, I want to create a new instance of these objects. My code doesn't seem to work. what's wrong with the follwing code?
procedure TVisionMail.AddAttachment(FileString: String);
var
I: Integer;
begin
     AttCount := AttCount + 1; // increment attachment count

     //set attachment file name
     if (AttCount <> 0) and (edAttachment.Text <> '') then
       edAttachment.text := edAttachment.text + ';';
     edAttachment.text := edAttachment.text + FileString;

     //move objects position down to allow space for attachment line
     VisionMail.Height := VisionMail.Height + 25;
     Panel1.Height     := Panel1.Height + 25;
     btnSend.Top       := btnSend.Top + 25;
     btnExit.Top       := btnExit.Top + 25;
     StatusMemo.Top    := StatusMemo.Top + 25;
     Memo1.Top         := Memo1.Top + 25;
     lblBody.Top       := lblBody.Top + 25;

       //Allocate memory for arrays
       SetLength(newImg, AttCount);
       SetLength(newlbl, AttCount);
       SetLength(newDel, AttCount);
       SetLength(newPin, AttCount);

        //create new instance and set parents, positions, color, events
        newImg[AttCount]:= TImage.Create(VisionMail);
        with newImg[AttCount] do
        begin
              Parent     := Panel1;
              Top        := Memo1.Top - 25;
              Left       := 408;
              Height     := 16;
              Width      := 16;
        end;
        newlbl[AttCount]:= TLabel.Create(VisionMail);
        with newlbl[AttCount] do
        begin
              Parent     := Panel1;
              Top        := newImg[I].Top + 2;
              Left       := 397;
              Height     := 3;
              Width      := 13;
              BiDiMode   := bdRightToLeft;
       end;
       newDel[AttCount] := TAdvToolButton.Create(VisionMail);
       with newDel[AttCount] do
        begin
              Parent       := Panel1;
              Top          := newImg[I].Top;
              Left         := 440;
              Height       := 16;
              Width        := 16;
              color        := clBtnFace;
              colorChecked := clBtnFace;
              colorDown    := clBtnFace;
              colorHot     := clBtnFace;
              OnClick      := btnDelAttClick;
              OnMouseEnter := btnDelAttMouseEnter;
              OnMouseLeave := btnDelAttMouseLeave;
       end;
       newPin[AttCount] := TImage.Create(VisionMail);
       with newDel[AttCount] do
        begin
              Parent     := Panel1;
              Top        := newImg[I].Top;
              Left       := 425;
              Height     := 16;
              Width      := 16;
       end;
       //get Icon for extension of file
       lstIcons.GetBitmap(GetIcon(ExtractFileExt
                          (OpenDialog1.FileName)),
                          newImg[AttCount].Picture.Bitmap);
       newlbl[AttCount].Caption    := ExtractFileName(FileString);

end; 


A: The most obvious flaw is that you are writing off the end of all of your arrays. For example, you write 
SetLength(newImg, AttCount);

and that means that the valid indices for newImg are 0 to AttCount-1 inclusive. But then you write
newImg[AttCount] := ...

and that is an out of bounds access because the last index is AttCount-1. You do the same for all your array access.
If you compile with range checking enabled, the compiler will generate a runtime error that explains what you have done wrong.
Personally I think you would be better using a record to hold your four components:
TAttachmentControls = record
  Img: TImage;
  Lbl: TLabel;
  .. etc.
end;

And use a TList<TAttachmentControls> as your container.
