mmmMange
asked on
Doesn't Free() and Release() check for nil-pointers?
Hi.
I'm creating a form at runtime, like this:
ControlWindow := TControlWIndow.Create(Main Window);
When I am done using the form, I call the Release() -method like this:
if Assigned(ControlWindow) then
ControlWindow.Release;
My question is this:
When I check the memoryusage via AllocMemCount, first it shows 452. After I have created ControlWindow it shows 683, and after I have Released ControlWIndow it shows 472. Why is there a difference of 20 (472 - 452)..? Shouldn't it show 452 after Release too?
I'm creating a form at runtime, like this:
ControlWindow := TControlWIndow.Create(Main
When I am done using the form, I call the Release() -method like this:
if Assigned(ControlWindow) then
ControlWindow.Release;
My question is this:
When I check the memoryusage via AllocMemCount, first it shows 452. After I have created ControlWindow it shows 683, and after I have Released ControlWIndow it shows 472. Why is there a difference of 20 (472 - 452)..? Shouldn't it show 452 after Release too?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW where is the connection to nil Pointers?
Let see an example:
//........ Project
program Q_21145490;
uses
Forms,
Unit1_Q_21145490 in 'Unit1_Q_21145490.pas' {Form1},
Unit2_Q_21145490 in 'Unit2_Q_21145490.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TFo rm1, Form1);
Application.Run;
end.
//........ Form1
unit Unit1_Q_21145490;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Unit2_Q_21145490;
procedure TForm1.SpeedButton1Click(S ender: TObject);
begin
Application.CreateForm(TFo rm2, Form2);
Form2.Show;
end;
procedure TForm1.SpeedButton2Click(S ender: TObject);
begin
if Assigned(Form2) then
Form2.Release;
if Assigned(Form2) then
Form2.Release;
end;
end.
//........ COMMENT:
Form2.Release; // does not make Form2 := nil;
so it is better to use:
procedure TForm1.SpeedButton2Click(S ender: TObject);
begin
if Assigned(Form2) then
begin
Form2.Release;
Form2 := nil;
end;
if Assigned(Form2) then
Form2.Release;
end;
Emil
//........ Project
program Q_21145490;
uses
Forms,
Unit1_Q_21145490 in 'Unit1_Q_21145490.pas' {Form1},
Unit2_Q_21145490 in 'Unit2_Q_21145490.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TFo
Application.Run;
end.
//........ Form1
unit Unit1_Q_21145490;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Unit2_Q_21145490;
procedure TForm1.SpeedButton1Click(S
begin
Application.CreateForm(TFo
Form2.Show;
end;
procedure TForm1.SpeedButton2Click(S
begin
if Assigned(Form2) then
Form2.Release;
if Assigned(Form2) then
Form2.Release;
end;
end.
//........ COMMENT:
Form2.Release; // does not make Form2 := nil;
so it is better to use:
procedure TForm1.SpeedButton2Click(S
begin
if Assigned(Form2) then
begin
Form2.Release;
Form2 := nil;
end;
if Assigned(Form2) then
Form2.Release;
end;
Emil
so it is better to use:
procedure TForm1.SpeedButton2Click(S ender: TObject);
begin
if Assigned(Form2) then
begin
Form2.Release;
Form2 := nil;
end;
end;
Emil
procedure TForm1.SpeedButton2Click(S
begin
if Assigned(Form2) then
begin
Form2.Release;
Form2 := nil;
end;
end;
Emil
it is better to use:
procedure TForm1.SpeedButton1Click(S ender: TObject);
begin
if not Assigned(Form2) then
Application.CreateForm(TFo rm2, Form2);
Form2.Show;
end;
procedure TForm1.SpeedButton1Click(S
begin
if not Assigned(Form2) then
Application.CreateForm(TFo
Form2.Show;
end;