?
Solved

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

Posted on 2004-09-26
5
Medium Priority
?
329 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 750 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

Technology Partners: 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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

771 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