Solved

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

Posted on 2004-09-26
5
318 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

17 Experts available now in Live!

Get 1:1 Help Now