Loading Images to AdvStringGrid

I am loading images into the AdvStringGrid directly from URL. 

In this method I use ADVStringGrid from TMS Components to display web images in the cells. 
Images are loaded via Indy component IdHTTP1 into a memory stream then I add them to array of JPEG images.
In the next step, I load them into Grid from Array of Pictures calling a method AddPictures from AdvStringGrid.

The problem I encouter is how to free the picture objects created every time a function is called without crashing the grid 
since it is still referring to them.

arrayPictures[j] := TPicture.Create;
I think I have to use a global variable to solve my problem, but maybe there is a better way. 
I did not post in on Google Plus but no one replied. 
Thanks


procedure TForm1.gridStationMasterClickCell(Sender: TObject; ARow,
  ACol: Integer);
var
  sqlQuery: string;
  oraStations : TOraQuery; 
  qryResults : TStringList;
  rows, cols, i, j : integer;
   imageUrlPath : string;
  arrayMS : array of TMemoryStream;
  arrayJpg : array of TJPEGImage;
  arrayPictures : array of TPicture;
  pictCounter : integer;


begin
  // Click on Photo Column
    if(ACol = 0) then
    begin
      sqlQuery := thumbNailsImagesSql + gridStationMaster.Cells[1,ARow] + '''';
      pictCounter := -1;
         try
            oraStations := getQueryResults(sqlQuery);
            rows := oraStations.RecordCount; // number of Records - Rows
            cols := oraStations.FieldCount;


            SetLength(arrayMS,cols);
            SetLength(arrayJpg,cols);
            SetLength(arrayPictures,cols);


            gridImageViewer.ClearAll;


            gridImageViewer.RowCount := cols;
            gridImageViewer.ColCount := rows;
 
             for i := 0 to rows-1 do
              begin
                for j := 0 to cols -1  do
                  begin
                    imageUrlPath :=  imageURL +oraStations.Fields.Fields[j].AsString; // URL path and image name
                    inc(pictCounter); // picture counter to eliminate empty cells if image is not found on the server
                    try
                      arrayMS[j] := TMemoryStream.Create;
                      arrayJpg[j] := TJPEGImage.Create;
                      arrayPictures[j] := TPicture.Create;


                      IdHTTP1.Get(imageUrlPath, arrayMS[j]);
                      if (arrayMS[j].Size > 0) then
                      begin
                      
                        //Store images in the array of MemoryStream objects
                        arrayMS[j].Seek(0,soFromBeginning);
                        //Store objects from Stream into array of JPEGS
                        arrayJpg[j].LoadFromStream(arrayMS[j]);
                        //Store JPEGs into the array of pictures in order to display them
                        arrayPictures[j].Assign(arrayJpg[j]);


                        gridImageViewer.AddPicture(pictCounter,0,arrayPictures[j],false,Shrink,0,haCenter,vaCenter);
                        
                      end; // end IF (arrayMS[j].Size > 0) 


                    arrayMS[j].Free;
                    arrayJpg[j].Free;


                    except
                      on E: EIdHTTPProtocolException do
                    begin
                      if E.ErrorCode = 404 then
                      begin
                        
                        arrayMS[j].Free;
                        arrayJpg[j].Free;
                        //arrayPictures[j].Free;  // TO DO


                        dec(pictCounter);
                        Continue;
                      end;
                    end; // end DO
                  end; // end TRY
                end; //end j loop - cols


                oraStations.Next;  // next record set
              end; // end i loop - rows
         finally
            oraStations.Session.Free;
            oraStations.Free;
         end;
    end; // end ACol = 0
    
  end;



When you use AddPicture(), the caller is responsible for destroying the picture and you'd typically do this after calling grid.RemovePicture.
If you want to avoid dealing with destroying these pictures, we'd recommend to use grid.CreatePicture() instead and then the grid will manage the lifetime of the picture itself and will destroy it when needed when the grid is destroyed.

Thank you. 

It should solve our problem. 
Regards
Chris