Solved

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

Posted on 2004-09-26
5
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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