#1 2011-02-17 21:30:54

migajek
Member
Registered: 2010-10-01
Posts: 89

TSQLRecord + TSQLTableToGrid - table freeing memory issue.

Hi,
I have a code as following:

private
 fRec: TSQLRecord;

// ...
begin
fRec:= TSQLPerson.CreateAndFillPrepare(dbClient, '');

fGridAdapter:= TSQLTableToGrid.Create(DrawGrid1, fRec.FillTable , dbClient);

now the problem is I can't free "fRec" on form destroy, because it also frees it's table - which is later freed by TSQLTableToGrid destructor.

So I end up with TSQLRecord leaking, or double freeing memory block ...

there should be way to tell one of those objects (probably TSQLTableToGrid ?) "hey, it's not your table, don't free it!!"

Or is there such flag somewhere?

Offline

#2 2011-02-18 10:51:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: TSQLRecord + TSQLTableToGrid - table freeing memory issue.

Two possibilities:

1. You just have to create your table "by hand", then associate it to the TSQLPerson instance:

aTable := dbClient.InternalListRecordsJSON(TSQLPerson,'');
fRec := TSQLPerson.Create;
fRec.FillPrepare(aTable);

2. You can use your code but unlink the table from fRec:

fRec:= TSQLPerson.CreateAndFillPrepare(dbClient, '');
fGridAdapter:= TSQLTableToGrid.Create(DrawGrid1, fRec.FillTable , dbClient);
fRec.FillTable.OwnerMustFree := false; // then the table must be freed independently to fRec

IMHO the first one is the better, because it's much easy to understand.

Offline

#3 2012-01-04 13:53:56

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: TSQLRecord + TSQLTableToGrid - table freeing memory issue.

I must extract data from database, then draw this data into TStringGrid.

I have try this code:

var
  Griglia: TSQLTableToGrid;
  Tabella: TSQLTableJSON;
  sql: string;
begin
  ...
  ...

  Tabella := Client.ExecuteList([], sql);

  if Tabella <> nil then
    try
      Griglia := TSQLTableToGrid.Create(StringGrid, Tabella, Client);
      Tabella.OwnerMustFree := False;
      Griglia.Free;
    finally
      Tabella.Free;
    end;
end;

If I use Griglia.Free; I get an error "Invalid pointer". How can I free all variables without problems?

Besides if I use Tabella.Free; my StringGrid is clear.

I need fill the grid the free all variables without lost grid data, is it possible?

A last think, after fill the grid if I use StringGrid.Cells[x,y] function to retrive a cell value I get always NULL string, I can see the value inside the string on the screen but I cannot get the value with StringGrid.Cells[x,y] function, why?

Last edited by array81 (2012-01-04 14:33:17)

Offline

Board footer

Powered by FluxBB