Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

memory leakage problem

Ok I need to know if there is any memory leakage in this code:

procedure TfrmMain.LoadDatFile;
var MyStream: TFileStream; FavRec: PFavoriteRec;
begin
  if FileExists('linkslist.dat') then begin
     MyStream := TFileStream.Create('linkslist.dat', fmOpenReadWrite);
     New(FavRec);

     MyStream.Seek(0, soFromBeginning);
     try
        while MyStream.Read(FavRec^, SizeOf(TFavoriteRec)) = SizeOf(TFavoriteRec) do begin
           tvFavs.Items[FavRec.AbsIndex].Data := FavRec;
           tvFavs.Items[FavRec.AbsIndex].ImageIndex := 2;
           tvFavs.Items[FavRec.AbsIndex].SelectedIndex := 2;
           New(FavRec);
        end;
     finally
        MyStream.Free;
     end;
  end;
end;

I'm allocating memory for those FavRecs, but I never use Dispose. And I'm not sure that when tvFavs (TreeView) is freed the .Data pointer is also freed.

many thanks.
0
pin_plunder
Asked:
pin_plunder
  • 3
1 Solution
 
EpsylonCommented:
You have to free it yourself...
0
 
pin_plunderAuthor Commented:
you mean, before closing my app something like this should execute:

Dispose(tvFavs.Items[i].Data)

thanks


0
 
EpsylonCommented:
Wll, when you close your app it will be freed. In fact all memory allocated with e.g. New or Allocmem, will be released automatically. But when you remove a treenode, you have to call Dispose to release the memory.
0
 
EpsylonCommented:
I mean that when you don't free the memory when you remove a treenode, it will not be released until you close your app.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now