[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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