Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Memory leak
Thu, Nov 11 2010 2:49 PMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Tim

One thing I would like to know is why the code below produces memory leak if
the scope of Table is local.

procedure test;
var Table: TDBISamTable;
begin
       Table := TDBISamTable.Create(nil);
       Table.TableName := 'x';
       Table.Open;
       // routine
end;

Look that there is neither a close nor free methods, but like I wrote above
why this is wrong ?

Actually I am using other aproach.

procedure test;
var Table: TDBISamTable;
begin
       Table := TDBISamTable.Create(nil);
       try
           Table.TableName := 'x';
           Table.Open;
           // routine
       finally
           Table.Close;
           Table.Free;
       end;
end;

Eduardo

Thu, Nov 11 2010 3:12 PMPermanent Link

Shane McCall

Eduardo,

You have specified no owner for the table, so you are responsible for
freeing the component when you are done with it.

Components free their owned components when they are released, but if a
component has no owner there is nothing that will release it automatically.

If you really want to keep this table open for the entire time the
application is active, use

Table := TDBISAMTable.Create(Application);

and it will be freed when the application is destroyed.

Sean

Eduardo [HPro] wrote:
> Tim
>
> One thing I would like to know is why the code below produces memory leak if
> the scope of Table is local.
>
> procedure test;
> var Table: TDBISamTable;
> begin
>         Table := TDBISamTable.Create(nil);
>         Table.TableName := 'x';
>         Table.Open;
>         // routine
> end;
>
> Look that there is neither a close nor free methods, but like I wrote above
> why this is wrong ?
>
> Actually I am using other aproach.
>
> procedure test;
> var Table: TDBISamTable;
> begin
>         Table := TDBISamTable.Create(nil);
>         try
>             Table.TableName := 'x';
>             Table.Open;
>             // routine
>         finally
>             Table.Close;
>             Table.Free;
>         end;
> end;
>
> Eduardo
>
>
Fri, Nov 12 2010 5:19 AMPermanent Link

John Hay

Eduardo

> One thing I would like to know is why the code below produces memory leak
if
> the scope of Table is local.
>
> procedure test;
> var Table: TDBISamTable;
> begin
>         Table := TDBISamTable.Create(nil);
>         Table.TableName := 'x';
>         Table.Open;
>         // routine
> end;
>
> Look that there is neither a close nor free methods, but like I wrote
above
> why this is wrong ?

The pointer Table is a local variable and its memory is reclaimed when the
procedure finishes.  The class TDBISamTable is created on the heap and its
memory is reclaimed when it is explicitly freed or its owner is freed.  With
a nil owner it is never freed.  Remember that Table is only a pointer to the
class, not the class itself.

John

Fri, Nov 12 2010 11:50 AMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

John and Sean

Thank you for both.

Eduardo

Image