TMSFMXGrid MergeCells memory leak

I found a memory leak in MergeCells and here's how I fixed it.  You might want to look at ClearCells as it does not call FreeControl whereas Clear does.

    for j := Row to Row + RowCount - 1 do
    begin
      if not usecp then
      begin
        cp := TCellProperty(IntObjects[i,j]);

        if not Assigned(cp) then
        begin                                                   // Added
          cp := TCellProperty.Create(GetDefaultFont(Col,Row));
          IntObjects[i,j] := cp;                                // Added
        end;                                                    // Added

        cp.BaseCol := Col;
        cp.BaseRow := Row;
        cp.ColSpan := ColCount;
        cp.RowSpan := RowCount;
        usecp := true;
      end
      else                                          // Added
        begin                                       // Added
          oldcp := TCellProperty(IntObjects[i,j]);  // Added
          if Assigned(oldcp) then                   // Added
          begin                                     // Added
            oldcp.FreeControl;                      // Added
            oldcp.Free;                             // Added
            oldcp := nil;                           // Added
          end;                                      // Added

          IntObjects[i,j] := cp;
        end;                                        // Added
    end;

I see that these memory leaks were not addressed in the latest updates.  Do you have plans to fixed them?

We were unable to reproduce this here, can you send us a sample that reproduces the memory leak?


Kind Regards, 
Pieter


1) I can produce the memory leak with the following code:

procedure TForm1.FormShow(Sender: TObject);
var
  c, r: integer;
begin
  ReportMemoryLeaksOnShutdown := True;

  for c := 0 to 5 do
    for r := 0 to 5 do
    begin
      TMSFMXGrid1.Cells[c,r] := IntToStr(c) + ',' + IntToStr(r);
      if Odd(r) then
        TMSFMXGrid1.Colors[c,r] := TAlphaColorRec.Red;
    end;

  TMSFMXGrid1.MergeCells(1,1,2,2);
end;

2) I added cp.FreeControl before the cp.Free call in ClearCells

3) I cleaned up the code for MergeCells

      if not usecp then
      begin
        cp := TCellProperty(IntObjects[i,j]);

        if not Assigned(cp) then
          cp := TCellProperty.Create(GetDefaultFont(RealToDisplColumn(Col),RealtoDisplRow(Row)));

        cp.BaseCol := Col;
        cp.BaseRow := Row;
        cp.ColSpan := ColCount;
        cp.RowSpan := RowCount;
        usecp := true;
      end
      else // Free old cp                          // Added
        begin                                      // Added
          oldcp := TCellProperty(IntObjects[i,j]); // Added
          if Assigned(oldcp) then                  // Added
          begin                                    // Added
            if Assigned(oldcp.Control) then        // Added
              oldcp.FreeControl;                   // Added
            oldcp.Free;                            // Added
          end;                                     // Added
        end;                                       // Added

Thanks for the feedback. We've applied the improvements and this will be included in the next update.

I'm getting bit by same bug while trying to merge cells. Could you specify which file was updated? I looked in FMX.TMSgrid but didn't find matching code
Thanks!
Neil

Are you using v3.1.1.1 as this issue was solved in this latest version update.

I actually did find the code, and yes am using 3.1.1.1 so did see the correction was already in place.
I was getting error during merge, and when saw message didn't realize the error discussed is actually different.- I'm getting a invalid pointer operation error on merging. I'll do some more investigation to see if I can identify exactly how and where its occurring.