We help IT Professionals succeed at work.

Help to free memory

Romans
Romans asked
on
Can somebody tell me the correct method to free all of the memory allocted in the Button1 click event in the
following code.  The code that I have in the FormClose event does not free up the memory correctly.

Help ....

Thanks



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  PAssy = ^TAssy;
  TAssy = record
    Part: String[15];
    Cost: String[15];
  end;

type
  PPlant = ^TPlant;
  TPlant = record
    PlantName: String[15];
    Assembly : TList;
  end;

var
  AssyList: TList;
  PlantList: TList;
  Assy: PAssy;
  Plant: PPlant;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
   New(Assy);
   Assy^.Part := 'Part1';
   Assy^.Cost := '$10.50';
   AssyList.Add(Assy);

   New(Plant);
   Plant^.PlantName := 'Colorado';
   Plant^.Assembly  := TList.Create;
   Plant^.Assembly.Add(Assy);
   PlantList.Add(Plant);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   AssyList  := TList.Create;
   PlantList := TList.Create;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
   I: Integer;
begin
   for I := 0 to AssyList.Count - 1 do
      Dispose(AssyList.Items[I]);
   for I := 0 to PlantList.Count - 1 do
      Dispose(PlantList.Items[I]);
   AssyList.Free;
   PlantList.Free;
end;

end.
Comment
Watch Question

Commented:
freeandnil(assylist);
freeandnil(plantlist);

Commented:
not sure which unit "freeandnil" is in

Author

Commented:
Is FreeAndNil a Delphi4 function .. ?  I think it is supposed to be in the SysUtils unit but I still get an error !

Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
to release the memory that allocated to pointor (using new) use Dispose

Dispose(Assy);
Dispose(Plant);

Commented:
hi,
ive tested your code and there are no leaks here.
freeandnil is in d5 in sysutils unit.
which pretty much does like below by putting the obj you created to nil then freeing it.

so the only other thing you could do is add the
PlantList := nil;
AssyList  := nil;
so the close looks like:


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  I: Integer;
begin
  for I := 0 to AssyList.Count - 1 do
     Dispose(AssyList.Items[I]);
  for I := 0 to PlantList.Count - 1 do
     Dispose(PlantList.Items[I]);
 PlantList := nil;
  AssyList := nil;
  AssyList.Free;
  PlantList.Free;
end;

Regards Barry

Commented:
try putting these code in the OnDestroy Event of the form.

plantlist.destroy;
assylist.destroy;
assylist := nil;
plantlist := nil;

Commented:
You're not freein the Assembly list properly. Even though the list's pointers (Assy) will be disposed of, the list itself is not explicitly freed. Try this:

var
  I: Integer;
begin
  for I := 0 to AssyList.Count - 1 do
    Dispose(PAssy(AssyList.Items[I]));
  for I := 0 to PlantList.Count - 1 do
  begin
    PPlant(PlantList.Items[I])^.Assembly.Free;
    Dispose(PPlant(PlantList.Items[I]));
  end;
  AssyList.Free;
  PlantList.Free;
end;

Barry, your code will result in an AV:

PlantList := nil;
AssyList := nil;
AssyList.Free; <-- already nil
PlantList.Free; <-- already nil

It's also a better idea to delete from bottom up:

var
  i: Integer;
begin
  if PlantList.Count > 0 then
    for i := PlantList.Count - 1 downto 0 do
      Dispose(PPlant(PlantList.Items[i]));
  PlantList.Free;
end;

Commented:
an AV i hope not ;-)
the delphi help under TObject.Free says
"Free is successful even if the object is nil so if the object was never initialized, Free won?t result in an error."
check the source code for freeandnil ,ive just done the same (it should be called nilandfree :-)

i guess nil doesnt matter here anyways as its on the formclose plantlist etc isnt going to be assigned
elsewhere..agree on deleting from count to 0 though.

Commented:
though how does freeandnil free something if its not pointing to it anymore?

Author

Commented:
Thanks for all of the help everybody. Rondi's code works best and checking with SnoopMonitor, frees all of the memory correctly. Note - I also checked this with MemProof and it indicates that all the memory has been freed.

http://www.automatedqa.com/downloads/memproof.asp 

Much appreciated Rondi !

Explore More ContentExplore courses, solutions, and other research materials related to this topic.