FNCDataGrid memory management issue with controls

Hello,

I have set up a blank project with a TMSFNCDataGrid on it and the following routines:

procedure TForm1.WebButton1Click(Sender: TObject);
begin
  TMSFNCDataGrid1.Free;
end;

procedure TForm1.WebFormCreate(Sender: TObject);
var
  col:TTMSFNCDataGridColumn;
  ed:TWebEdit;
  i:Integer;
begin
  TMSFNCDataGrid1.BeginUpdate;

  col:=TMSFNCDataGrid1.Columns.Add;
  for i:=TMSFNCDataGrid1.FixedRowCount to TMSFNCDataGrid1.RowCount-1 do
    begin
      ed:=TWebEdit.Create(TMSFNCDataGrid1);
      ed.Text:='1';
      ed.EditType:=weNumeric;
      TMSFNCDataGrid1.Controls[col.index, i]:=ed;
      TMSFNCDataGrid1.ControlAligns[col.index, i]:=gcaClient;
    end;

  TMSFNCDataGrid1.EndUpdate;
end;

When the datagrid is freed, an exception is raised, which I believe to pertain to memory management.

I am relatively sure that this used to compile (within the last 3 months) and stopped after a recent update?

On another note, I have seen in another thread that there is the ManagedObjects property; is this a better way of handling memory?

Thanks!

Hi,

You can indeed use managed objects, and if the controls property is used, you don't need to set an owner. because when destroying the data grid, the owner will clean up the objects and because there is still a reference in Controls that's where the error comes from. You can use this code to avoid errors:

var
  col:TTMSFNCDataGridColumn;
  ed:TWebEdit;
  i:Integer;
begin
  TMSFNCDataGrid1.BeginUpdate;

  col:=TMSFNCDataGrid1.Columns.Add;
  for i:=TMSFNCDataGrid1.FixedRowCount to TMSFNCDataGrid1.RowCount-1 do
    begin
      ed:=TWebEdit.Create(nil);
      ed.Text:='1';
      ed.EditType:=weNumeric;
      TMSFNCDataGrid1.ManagedObjects[col.index, i] := ed;
      TMSFNCDataGrid1.Controls[col.index, i]:=ed;
      TMSFNCDataGrid1.ControlAligns[col.index, i]:=gcaClient;
    end;

  TMSFNCDataGrid1.EndUpdate;

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.