Solved

Doesn't Free() and Release() check for nil-pointers?

Posted on 2004-09-26
5
320 Views
Last Modified: 2010-04-05
Hi.

I'm creating a form at runtime, like this:

ControlWindow := TControlWIndow.Create(MainWindow);

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?
0
Comment
Question by:mmmMange
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 250 total points
ID: 12154985
Not very likely.
The memory management needs memory to hold the list of free memory.
The allocated memory has been chipped off a bigger block. When freeing part of the the bigger block has been allocated
and the freed block cannot be united with it. The memory to handle the freed entry is taken from the freed block itself.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 12154995
BTW where is the connection to nil Pointers?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12154997
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(TForm1, 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(Sender: TObject);
begin
  Application.CreateForm(TForm2, Form2);
  Form2.Show;
end;

procedure TForm1.SpeedButton2Click(Sender: 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(Sender: TObject);
begin
  if Assigned(Form2) then
  begin
    Form2.Release;
    Form2 := nil;
  end;
  if Assigned(Form2) then
    Form2.Release;
end;

Emil
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12154999
so it is better to use:

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  if Assigned(Form2) then
  begin
    Form2.Release;
    Form2 := nil;
  end;
end;

Emil
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12155138
it is better to use:

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if not Assigned(Form2) then
    Application.CreateForm(TForm2, Form2);
  Form2.Show;
end;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now